arc/ppc-sam460: Interrupt and scheduling fixes
[AROS.git] / arch / ppc-sam440 / kernel / kernel_cpu.c
blobc27310c7b653d5affe8c4812f8ae7830f7f69ae4
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <aros/debug.h>
8 #include <asm/amcc440.h>
9 #include <aros/kernel.h>
10 #include <aros/libcall.h>
11 #include <exec/execbase.h>
12 #include <hardware/intbits.h>
14 #include "etask.h"
16 #include "kernel_intern.h"
17 #include "kernel_cpu.h"
18 #include "kernel_syscall.h"
19 #include "kernel_scheduler.h"
20 #include "kernel_intr.h"
22 #include <strings.h>
25 * Task dispatcher. Basically it may be the same one no matter what scheduling algorithm is used
27 void cpu_Dispatch(context_t *regs)
29 struct Task *task;
30 uint64_t idle;
31 extern uint64_t idle_time;
33 __asm__ __volatile__("wrteei 0;");
34 idle = mftbu();
36 /*
37 * Is the list of ready tasks empty? Well, increment the idle switch cound and halt CPU.
38 * It should be extended by some plugin mechanism which would put CPU and whole machine
39 * into some more sophisticated sleep states (ACPI?)
42 while (!(task = core_Dispatch())) {
43 wrmsr(rdmsr() | MSR_POW | MSR_EE);
44 __asm__ __volatile__("sync; isync;");
45 __asm__ __volatile__("wrteei 0");
46 idle_time += mftbu() - idle;
48 if (SysBase->SysFlags & SFF_SoftInt)
49 core_Cause(INTB_SOFTINT, 1l << INTB_SOFTINT);
52 /* Restore the task's state */
53 CopyMem(task->tc_UnionETask.tc_ETask->et_RegFrame, regs, sizeof(regs_t));
54 regs->cpu.gpr[1] = (IPTR)task->tc_SPReg;
56 /* Handle tasks's flags */
57 if (task->tc_Flags & TF_EXCEPT)
58 Exception();
60 /* Store the launch time */
61 GetIntETask(task)->iet_private1 = mftbu();
63 if (task->tc_Flags & TF_LAUNCH)
65 AROS_UFC1(void, task->tc_Launch,
66 AROS_UFCA(struct ExecBase *, SysBase, A6));
69 /* Copy the fpu, mmx, xmm state */
70 // FIXME: Change to the lazy saving of the FPU state!!!!
72 regs->cpu.srr1 |= MSR_EE;
75 void cpu_Switch(context_t *regs)
77 struct Task *task;
79 /* Disable interrupts until the task switch */
80 __asm__ __volatile__("wrteei 0");
82 task = SysBase->ThisTask;
84 /* Copy current task's context into the ETask structure */
85 memmove(task->tc_UnionETask.tc_ETask->et_RegFrame, regs, sizeof(context_t));
87 /* Copy the fpu, mmx, xmm state */
88 // FIXME: Change to the lazy saving of the FPU state!!!!
90 task->tc_SPReg = (APTR)regs->cpu.gpr[1];
92 core_Switch();