]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/unicore32/kernel/process.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[karo-tx-linux.git] / arch / unicore32 / kernel / process.c
1 /*
2  * linux/arch/unicore32/kernel/process.c
3  *
4  * Code specific to PKUnity SoC and UniCore ISA
5  *
6  * Copyright (C) 2001-2010 GUAN Xue-tao
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 #include <stdarg.h>
13
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/stddef.h>
19 #include <linux/unistd.h>
20 #include <linux/delay.h>
21 #include <linux/reboot.h>
22 #include <linux/interrupt.h>
23 #include <linux/kallsyms.h>
24 #include <linux/init.h>
25 #include <linux/cpu.h>
26 #include <linux/elfcore.h>
27 #include <linux/pm.h>
28 #include <linux/tick.h>
29 #include <linux/utsname.h>
30 #include <linux/uaccess.h>
31 #include <linux/random.h>
32 #include <linux/gpio.h>
33 #include <linux/stacktrace.h>
34
35 #include <asm/cacheflush.h>
36 #include <asm/processor.h>
37 #include <asm/stacktrace.h>
38
39 #include "setup.h"
40
41 static const char * const processor_modes[] = {
42         "UK00", "UK01", "UK02", "UK03", "UK04", "UK05", "UK06", "UK07",
43         "UK08", "UK09", "UK0A", "UK0B", "UK0C", "UK0D", "UK0E", "UK0F",
44         "USER", "REAL", "INTR", "PRIV", "UK14", "UK15", "UK16", "ABRT",
45         "UK18", "UK19", "UK1A", "EXTN", "UK1C", "UK1D", "UK1E", "SUSR"
46 };
47
48 void cpu_idle(void)
49 {
50         /* endless idle loop with no priority at all */
51         while (1) {
52                 tick_nohz_idle_enter();
53                 rcu_idle_enter();
54                 while (!need_resched()) {
55                         local_irq_disable();
56                         stop_critical_timings();
57                         cpu_do_idle();
58                         local_irq_enable();
59                         start_critical_timings();
60                 }
61                 rcu_idle_exit();
62                 tick_nohz_idle_exit();
63                 preempt_enable_no_resched();
64                 schedule();
65                 preempt_disable();
66         }
67 }
68
69 static char reboot_mode = 'h';
70
71 int __init reboot_setup(char *str)
72 {
73         reboot_mode = str[0];
74         return 1;
75 }
76
77 __setup("reboot=", reboot_setup);
78
79 void machine_halt(void)
80 {
81         gpio_set_value(GPO_SOFT_OFF, 0);
82 }
83
84 /*
85  * Function pointers to optional machine specific functions
86  */
87 void (*pm_power_off)(void) = NULL;
88
89 void machine_power_off(void)
90 {
91         if (pm_power_off)
92                 pm_power_off();
93         machine_halt();
94 }
95
96 void machine_restart(char *cmd)
97 {
98         /* Disable interrupts first */
99         local_irq_disable();
100
101         /*
102          * Tell the mm system that we are going to reboot -
103          * we may need it to insert some 1:1 mappings so that
104          * soft boot works.
105          */
106         setup_mm_for_reboot(reboot_mode);
107
108         /* Clean and invalidate caches */
109         flush_cache_all();
110
111         /* Turn off caching */
112         cpu_proc_fin();
113
114         /* Push out any further dirty data, and ensure cache is empty */
115         flush_cache_all();
116
117         /*
118          * Now handle reboot code.
119          */
120         if (reboot_mode == 's') {
121                 /* Jump into ROM at address 0xffff0000 */
122                 cpu_reset(VECTORS_BASE);
123         } else {
124                 writel(0x00002001, PM_PLLSYSCFG); /* cpu clk = 250M */
125                 writel(0x00100800, PM_PLLDDRCFG); /* ddr clk =  44M */
126                 writel(0x00002001, PM_PLLVGACFG); /* vga clk = 250M */
127
128                 /* Use on-chip reset capability */
129                 /* following instructions must be in one icache line */
130                 __asm__ __volatile__(
131                         "       .align 5\n\t"
132                         "       stw     %1, [%0]\n\t"
133                         "201:   ldw     r0, [%0]\n\t"
134                         "       cmpsub.a        r0, #0\n\t"
135                         "       bne     201b\n\t"
136                         "       stw     %3, [%2]\n\t"
137                         "       nop; nop; nop\n\t"
138                         /* prefetch 3 instructions at most */
139                         :
140                         : "r" (PM_PMCR),
141                           "r" (PM_PMCR_CFBSYS | PM_PMCR_CFBDDR
142                                 | PM_PMCR_CFBVGA),
143                           "r" (RESETC_SWRR),
144                           "r" (RESETC_SWRR_SRB)
145                         : "r0", "memory");
146         }
147
148         /*
149          * Whoops - the architecture was unable to reboot.
150          * Tell the user!
151          */
152         mdelay(1000);
153         printk(KERN_EMERG "Reboot failed -- System halted\n");
154         do { } while (1);
155 }
156
157 void __show_regs(struct pt_regs *regs)
158 {
159         unsigned long flags;
160         char buf[64];
161
162         printk(KERN_DEFAULT "CPU: %d    %s  (%s %.*s)\n",
163                 raw_smp_processor_id(), print_tainted(),
164                 init_utsname()->release,
165                 (int)strcspn(init_utsname()->version, " "),
166                 init_utsname()->version);
167         print_symbol("PC is at %s\n", instruction_pointer(regs));
168         print_symbol("LR is at %s\n", regs->UCreg_lr);
169         printk(KERN_DEFAULT "pc : [<%08lx>]    lr : [<%08lx>]    psr: %08lx\n"
170                "sp : %08lx  ip : %08lx  fp : %08lx\n",
171                 regs->UCreg_pc, regs->UCreg_lr, regs->UCreg_asr,
172                 regs->UCreg_sp, regs->UCreg_ip, regs->UCreg_fp);
173         printk(KERN_DEFAULT "r26: %08lx  r25: %08lx  r24: %08lx\n",
174                 regs->UCreg_26, regs->UCreg_25,
175                 regs->UCreg_24);
176         printk(KERN_DEFAULT "r23: %08lx  r22: %08lx  r21: %08lx  r20: %08lx\n",
177                 regs->UCreg_23, regs->UCreg_22,
178                 regs->UCreg_21, regs->UCreg_20);
179         printk(KERN_DEFAULT "r19: %08lx  r18: %08lx  r17: %08lx  r16: %08lx\n",
180                 regs->UCreg_19, regs->UCreg_18,
181                 regs->UCreg_17, regs->UCreg_16);
182         printk(KERN_DEFAULT "r15: %08lx  r14: %08lx  r13: %08lx  r12: %08lx\n",
183                 regs->UCreg_15, regs->UCreg_14,
184                 regs->UCreg_13, regs->UCreg_12);
185         printk(KERN_DEFAULT "r11: %08lx  r10: %08lx  r9 : %08lx  r8 : %08lx\n",
186                 regs->UCreg_11, regs->UCreg_10,
187                 regs->UCreg_09, regs->UCreg_08);
188         printk(KERN_DEFAULT "r7 : %08lx  r6 : %08lx  r5 : %08lx  r4 : %08lx\n",
189                 regs->UCreg_07, regs->UCreg_06,
190                 regs->UCreg_05, regs->UCreg_04);
191         printk(KERN_DEFAULT "r3 : %08lx  r2 : %08lx  r1 : %08lx  r0 : %08lx\n",
192                 regs->UCreg_03, regs->UCreg_02,
193                 regs->UCreg_01, regs->UCreg_00);
194
195         flags = regs->UCreg_asr;
196         buf[0] = flags & PSR_S_BIT ? 'S' : 's';
197         buf[1] = flags & PSR_Z_BIT ? 'Z' : 'z';
198         buf[2] = flags & PSR_C_BIT ? 'C' : 'c';
199         buf[3] = flags & PSR_V_BIT ? 'V' : 'v';
200         buf[4] = '\0';
201
202         printk(KERN_DEFAULT "Flags: %s  INTR o%s  REAL o%s  Mode %s  Segment %s\n",
203                 buf, interrupts_enabled(regs) ? "n" : "ff",
204                 fast_interrupts_enabled(regs) ? "n" : "ff",
205                 processor_modes[processor_mode(regs)],
206                 segment_eq(get_fs(), get_ds()) ? "kernel" : "user");
207         {
208                 unsigned int ctrl;
209
210                 buf[0] = '\0';
211                 {
212                         unsigned int transbase;
213                         asm("movc %0, p0.c2, #0\n"
214                             : "=r" (transbase));
215                         snprintf(buf, sizeof(buf), "  Table: %08x", transbase);
216                 }
217                 asm("movc %0, p0.c1, #0\n" : "=r" (ctrl));
218
219                 printk(KERN_DEFAULT "Control: %08x%s\n", ctrl, buf);
220         }
221 }
222
223 void show_regs(struct pt_regs *regs)
224 {
225         printk(KERN_DEFAULT "\n");
226         printk(KERN_DEFAULT "Pid: %d, comm: %20s\n",
227                         task_pid_nr(current), current->comm);
228         __show_regs(regs);
229         __backtrace();
230 }
231
232 /*
233  * Free current thread data structures etc..
234  */
235 void exit_thread(void)
236 {
237 }
238
239 void flush_thread(void)
240 {
241         struct thread_info *thread = current_thread_info();
242         struct task_struct *tsk = current;
243
244         memset(thread->used_cp, 0, sizeof(thread->used_cp));
245         memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
246 #ifdef CONFIG_UNICORE_FPU_F64
247         memset(&thread->fpstate, 0, sizeof(struct fp_state));
248 #endif
249 }
250
251 void release_thread(struct task_struct *dead_task)
252 {
253 }
254
255 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
256 asmlinkage void ret_from_kernel_thread(void) __asm__("ret_from_kernel_thread");
257
258 int
259 copy_thread(unsigned long clone_flags, unsigned long stack_start,
260             unsigned long stk_sz, struct task_struct *p)
261 {
262         struct thread_info *thread = task_thread_info(p);
263         struct pt_regs *childregs = task_pt_regs(p);
264
265         memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save));
266         thread->cpu_context.sp = (unsigned long)childregs;
267         if (unlikely(p->flags & PF_KTHREAD)) {
268                 thread->cpu_context.pc = (unsigned long)ret_from_kernel_thread;
269                 thread->cpu_context.r4 = stack_start;
270                 thread->cpu_context.r5 = stk_sz;
271                 memset(childregs, 0, sizeof(struct pt_regs));
272         } else {
273                 thread->cpu_context.pc = (unsigned long)ret_from_fork;
274                 *childregs = *current_pt_regs();
275                 childregs->UCreg_00 = 0;
276                 if (stack_start)
277                         childregs->UCreg_sp = stack_start;
278
279                 if (clone_flags & CLONE_SETTLS)
280                         childregs->UCreg_16 = childregs->UCreg_03;
281         }
282         return 0;
283 }
284
285 /*
286  * Fill in the task's elfregs structure for a core dump.
287  */
288 int dump_task_regs(struct task_struct *t, elf_gregset_t *elfregs)
289 {
290         elf_core_copy_regs(elfregs, task_pt_regs(t));
291         return 1;
292 }
293
294 /*
295  * fill in the fpe structure for a core dump...
296  */
297 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fp)
298 {
299         struct thread_info *thread = current_thread_info();
300         int used_math = thread->used_cp[1] | thread->used_cp[2];
301
302 #ifdef CONFIG_UNICORE_FPU_F64
303         if (used_math)
304                 memcpy(fp, &thread->fpstate, sizeof(*fp));
305 #endif
306         return used_math != 0;
307 }
308 EXPORT_SYMBOL(dump_fpu);
309
310 unsigned long get_wchan(struct task_struct *p)
311 {
312         struct stackframe frame;
313         int count = 0;
314         if (!p || p == current || p->state == TASK_RUNNING)
315                 return 0;
316
317         frame.fp = thread_saved_fp(p);
318         frame.sp = thread_saved_sp(p);
319         frame.lr = 0;                   /* recovered from the stack */
320         frame.pc = thread_saved_pc(p);
321         do {
322                 int ret = unwind_frame(&frame);
323                 if (ret < 0)
324                         return 0;
325                 if (!in_sched_functions(frame.pc))
326                         return frame.pc;
327         } while ((count++) < 16);
328         return 0;
329 }
330
331 unsigned long arch_randomize_brk(struct mm_struct *mm)
332 {
333         unsigned long range_end = mm->brk + 0x02000000;
334         return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
335 }
336
337 /*
338  * The vectors page is always readable from user space for the
339  * atomic helpers and the signal restart code.  Let's declare a mapping
340  * for it so it is visible through ptrace and /proc/<pid>/mem.
341  */
342
343 int vectors_user_mapping(void)
344 {
345         struct mm_struct *mm = current->mm;
346         return install_special_mapping(mm, 0xffff0000, PAGE_SIZE,
347                                        VM_READ | VM_EXEC |
348                                        VM_MAYREAD | VM_MAYEXEC |
349                                        VM_DONTEXPAND | VM_DONTDUMP,
350                                        NULL);
351 }
352
353 const char *arch_vma_name(struct vm_area_struct *vma)
354 {
355         return (vma->vm_start == 0xffff0000) ? "[vectors]" : NULL;
356 }