]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/unicore32/kernel/process.c
reboot: unicore32: prepare reboot_mode for moving to generic kernel code
[mirror_ubuntu-bionic-kernel.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 arch_cpu_idle(void)
49 {
50 cpu_do_idle();
51 local_irq_enable();
52 }
53
54 static enum reboot_mode reboot_mode = REBOOT_HARD;
55
56 int __init reboot_setup(char *str)
57 {
58 if ('s' == str[0])
59 reboot_mode = REBOOT_SOFT;
60 return 1;
61 }
62 __setup("reboot=", reboot_setup);
63
64 void machine_halt(void)
65 {
66 gpio_set_value(GPO_SOFT_OFF, 0);
67 }
68
69 /*
70 * Function pointers to optional machine specific functions
71 */
72 void (*pm_power_off)(void) = NULL;
73
74 void machine_power_off(void)
75 {
76 if (pm_power_off)
77 pm_power_off();
78 machine_halt();
79 }
80
81 void machine_restart(char *cmd)
82 {
83 /* Disable interrupts first */
84 local_irq_disable();
85
86 /*
87 * Tell the mm system that we are going to reboot -
88 * we may need it to insert some 1:1 mappings so that
89 * soft boot works.
90 */
91 setup_mm_for_reboot();
92
93 /* Clean and invalidate caches */
94 flush_cache_all();
95
96 /* Turn off caching */
97 cpu_proc_fin();
98
99 /* Push out any further dirty data, and ensure cache is empty */
100 flush_cache_all();
101
102 /*
103 * Now handle reboot code.
104 */
105 if (reboot_mode == REBOOT_SOFT) {
106 /* Jump into ROM at address 0xffff0000 */
107 cpu_reset(VECTORS_BASE);
108 } else {
109 writel(0x00002001, PM_PLLSYSCFG); /* cpu clk = 250M */
110 writel(0x00100800, PM_PLLDDRCFG); /* ddr clk = 44M */
111 writel(0x00002001, PM_PLLVGACFG); /* vga clk = 250M */
112
113 /* Use on-chip reset capability */
114 /* following instructions must be in one icache line */
115 __asm__ __volatile__(
116 " .align 5\n\t"
117 " stw %1, [%0]\n\t"
118 "201: ldw r0, [%0]\n\t"
119 " cmpsub.a r0, #0\n\t"
120 " bne 201b\n\t"
121 " stw %3, [%2]\n\t"
122 " nop; nop; nop\n\t"
123 /* prefetch 3 instructions at most */
124 :
125 : "r" (PM_PMCR),
126 "r" (PM_PMCR_CFBSYS | PM_PMCR_CFBDDR
127 | PM_PMCR_CFBVGA),
128 "r" (RESETC_SWRR),
129 "r" (RESETC_SWRR_SRB)
130 : "r0", "memory");
131 }
132
133 /*
134 * Whoops - the architecture was unable to reboot.
135 * Tell the user!
136 */
137 mdelay(1000);
138 printk(KERN_EMERG "Reboot failed -- System halted\n");
139 do { } while (1);
140 }
141
142 void __show_regs(struct pt_regs *regs)
143 {
144 unsigned long flags;
145 char buf[64];
146
147 show_regs_print_info(KERN_DEFAULT);
148 print_symbol("PC is at %s\n", instruction_pointer(regs));
149 print_symbol("LR is at %s\n", regs->UCreg_lr);
150 printk(KERN_DEFAULT "pc : [<%08lx>] lr : [<%08lx>] psr: %08lx\n"
151 "sp : %08lx ip : %08lx fp : %08lx\n",
152 regs->UCreg_pc, regs->UCreg_lr, regs->UCreg_asr,
153 regs->UCreg_sp, regs->UCreg_ip, regs->UCreg_fp);
154 printk(KERN_DEFAULT "r26: %08lx r25: %08lx r24: %08lx\n",
155 regs->UCreg_26, regs->UCreg_25,
156 regs->UCreg_24);
157 printk(KERN_DEFAULT "r23: %08lx r22: %08lx r21: %08lx r20: %08lx\n",
158 regs->UCreg_23, regs->UCreg_22,
159 regs->UCreg_21, regs->UCreg_20);
160 printk(KERN_DEFAULT "r19: %08lx r18: %08lx r17: %08lx r16: %08lx\n",
161 regs->UCreg_19, regs->UCreg_18,
162 regs->UCreg_17, regs->UCreg_16);
163 printk(KERN_DEFAULT "r15: %08lx r14: %08lx r13: %08lx r12: %08lx\n",
164 regs->UCreg_15, regs->UCreg_14,
165 regs->UCreg_13, regs->UCreg_12);
166 printk(KERN_DEFAULT "r11: %08lx r10: %08lx r9 : %08lx r8 : %08lx\n",
167 regs->UCreg_11, regs->UCreg_10,
168 regs->UCreg_09, regs->UCreg_08);
169 printk(KERN_DEFAULT "r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
170 regs->UCreg_07, regs->UCreg_06,
171 regs->UCreg_05, regs->UCreg_04);
172 printk(KERN_DEFAULT "r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
173 regs->UCreg_03, regs->UCreg_02,
174 regs->UCreg_01, regs->UCreg_00);
175
176 flags = regs->UCreg_asr;
177 buf[0] = flags & PSR_S_BIT ? 'S' : 's';
178 buf[1] = flags & PSR_Z_BIT ? 'Z' : 'z';
179 buf[2] = flags & PSR_C_BIT ? 'C' : 'c';
180 buf[3] = flags & PSR_V_BIT ? 'V' : 'v';
181 buf[4] = '\0';
182
183 printk(KERN_DEFAULT "Flags: %s INTR o%s REAL o%s Mode %s Segment %s\n",
184 buf, interrupts_enabled(regs) ? "n" : "ff",
185 fast_interrupts_enabled(regs) ? "n" : "ff",
186 processor_modes[processor_mode(regs)],
187 segment_eq(get_fs(), get_ds()) ? "kernel" : "user");
188 {
189 unsigned int ctrl;
190
191 buf[0] = '\0';
192 {
193 unsigned int transbase;
194 asm("movc %0, p0.c2, #0\n"
195 : "=r" (transbase));
196 snprintf(buf, sizeof(buf), " Table: %08x", transbase);
197 }
198 asm("movc %0, p0.c1, #0\n" : "=r" (ctrl));
199
200 printk(KERN_DEFAULT "Control: %08x%s\n", ctrl, buf);
201 }
202 }
203
204 void show_regs(struct pt_regs *regs)
205 {
206 printk(KERN_DEFAULT "\n");
207 printk(KERN_DEFAULT "Pid: %d, comm: %20s\n",
208 task_pid_nr(current), current->comm);
209 __show_regs(regs);
210 __backtrace();
211 }
212
213 /*
214 * Free current thread data structures etc..
215 */
216 void exit_thread(void)
217 {
218 }
219
220 void flush_thread(void)
221 {
222 struct thread_info *thread = current_thread_info();
223 struct task_struct *tsk = current;
224
225 memset(thread->used_cp, 0, sizeof(thread->used_cp));
226 memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
227 #ifdef CONFIG_UNICORE_FPU_F64
228 memset(&thread->fpstate, 0, sizeof(struct fp_state));
229 #endif
230 }
231
232 void release_thread(struct task_struct *dead_task)
233 {
234 }
235
236 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
237 asmlinkage void ret_from_kernel_thread(void) __asm__("ret_from_kernel_thread");
238
239 int
240 copy_thread(unsigned long clone_flags, unsigned long stack_start,
241 unsigned long stk_sz, struct task_struct *p)
242 {
243 struct thread_info *thread = task_thread_info(p);
244 struct pt_regs *childregs = task_pt_regs(p);
245
246 memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save));
247 thread->cpu_context.sp = (unsigned long)childregs;
248 if (unlikely(p->flags & PF_KTHREAD)) {
249 thread->cpu_context.pc = (unsigned long)ret_from_kernel_thread;
250 thread->cpu_context.r4 = stack_start;
251 thread->cpu_context.r5 = stk_sz;
252 memset(childregs, 0, sizeof(struct pt_regs));
253 } else {
254 thread->cpu_context.pc = (unsigned long)ret_from_fork;
255 *childregs = *current_pt_regs();
256 childregs->UCreg_00 = 0;
257 if (stack_start)
258 childregs->UCreg_sp = stack_start;
259
260 if (clone_flags & CLONE_SETTLS)
261 childregs->UCreg_16 = childregs->UCreg_03;
262 }
263 return 0;
264 }
265
266 /*
267 * Fill in the task's elfregs structure for a core dump.
268 */
269 int dump_task_regs(struct task_struct *t, elf_gregset_t *elfregs)
270 {
271 elf_core_copy_regs(elfregs, task_pt_regs(t));
272 return 1;
273 }
274
275 /*
276 * fill in the fpe structure for a core dump...
277 */
278 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fp)
279 {
280 struct thread_info *thread = current_thread_info();
281 int used_math = thread->used_cp[1] | thread->used_cp[2];
282
283 #ifdef CONFIG_UNICORE_FPU_F64
284 if (used_math)
285 memcpy(fp, &thread->fpstate, sizeof(*fp));
286 #endif
287 return used_math != 0;
288 }
289 EXPORT_SYMBOL(dump_fpu);
290
291 unsigned long get_wchan(struct task_struct *p)
292 {
293 struct stackframe frame;
294 int count = 0;
295 if (!p || p == current || p->state == TASK_RUNNING)
296 return 0;
297
298 frame.fp = thread_saved_fp(p);
299 frame.sp = thread_saved_sp(p);
300 frame.lr = 0; /* recovered from the stack */
301 frame.pc = thread_saved_pc(p);
302 do {
303 int ret = unwind_frame(&frame);
304 if (ret < 0)
305 return 0;
306 if (!in_sched_functions(frame.pc))
307 return frame.pc;
308 } while ((count++) < 16);
309 return 0;
310 }
311
312 unsigned long arch_randomize_brk(struct mm_struct *mm)
313 {
314 unsigned long range_end = mm->brk + 0x02000000;
315 return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
316 }
317
318 /*
319 * The vectors page is always readable from user space for the
320 * atomic helpers and the signal restart code. Let's declare a mapping
321 * for it so it is visible through ptrace and /proc/<pid>/mem.
322 */
323
324 int vectors_user_mapping(void)
325 {
326 struct mm_struct *mm = current->mm;
327 return install_special_mapping(mm, 0xffff0000, PAGE_SIZE,
328 VM_READ | VM_EXEC |
329 VM_MAYREAD | VM_MAYEXEC |
330 VM_DONTEXPAND | VM_DONTDUMP,
331 NULL);
332 }
333
334 const char *arch_vma_name(struct vm_area_struct *vma)
335 {
336 return (vma->vm_start == 0xffff0000) ? "[vectors]" : NULL;
337 }