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