xref: /linux/arch/microblaze/kernel/process.c (revision 3a598264436e94c410c413088a7873fcad33616c)
1 /*
2  * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
3  * Copyright (C) 2008-2009 PetaLogix
4  * Copyright (C) 2006 Atmark Techno, Inc.
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License. See the file "COPYING" in the main directory of this archive
8  * for more details.
9  */
10 
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/pm.h>
14 #include <linux/tick.h>
15 #include <linux/bitops.h>
16 #include <asm/system.h>
17 #include <asm/pgalloc.h>
18 #include <asm/uaccess.h> /* for USER_DS macros */
19 #include <asm/cacheflush.h>
20 
21 void show_regs(struct pt_regs *regs)
22 {
23 	printk(KERN_INFO " Registers dump: mode=%X\r\n", regs->pt_mode);
24 	printk(KERN_INFO " r1=%08lX, r2=%08lX, r3=%08lX, r4=%08lX\n",
25 				regs->r1, regs->r2, regs->r3, regs->r4);
26 	printk(KERN_INFO " r5=%08lX, r6=%08lX, r7=%08lX, r8=%08lX\n",
27 				regs->r5, regs->r6, regs->r7, regs->r8);
28 	printk(KERN_INFO " r9=%08lX, r10=%08lX, r11=%08lX, r12=%08lX\n",
29 				regs->r9, regs->r10, regs->r11, regs->r12);
30 	printk(KERN_INFO " r13=%08lX, r14=%08lX, r15=%08lX, r16=%08lX\n",
31 				regs->r13, regs->r14, regs->r15, regs->r16);
32 	printk(KERN_INFO " r17=%08lX, r18=%08lX, r19=%08lX, r20=%08lX\n",
33 				regs->r17, regs->r18, regs->r19, regs->r20);
34 	printk(KERN_INFO " r21=%08lX, r22=%08lX, r23=%08lX, r24=%08lX\n",
35 				regs->r21, regs->r22, regs->r23, regs->r24);
36 	printk(KERN_INFO " r25=%08lX, r26=%08lX, r27=%08lX, r28=%08lX\n",
37 				regs->r25, regs->r26, regs->r27, regs->r28);
38 	printk(KERN_INFO " r29=%08lX, r30=%08lX, r31=%08lX, rPC=%08lX\n",
39 				regs->r29, regs->r30, regs->r31, regs->pc);
40 	printk(KERN_INFO " msr=%08lX, ear=%08lX, esr=%08lX, fsr=%08lX\n",
41 				regs->msr, regs->ear, regs->esr, regs->fsr);
42 }
43 
44 void (*pm_idle)(void);
45 void (*pm_power_off)(void) = NULL;
46 EXPORT_SYMBOL(pm_power_off);
47 
48 static int hlt_counter = 1;
49 
50 void disable_hlt(void)
51 {
52 	hlt_counter++;
53 }
54 EXPORT_SYMBOL(disable_hlt);
55 
56 void enable_hlt(void)
57 {
58 	hlt_counter--;
59 }
60 EXPORT_SYMBOL(enable_hlt);
61 
62 static int __init nohlt_setup(char *__unused)
63 {
64 	hlt_counter = 1;
65 	return 1;
66 }
67 __setup("nohlt", nohlt_setup);
68 
69 static int __init hlt_setup(char *__unused)
70 {
71 	hlt_counter = 0;
72 	return 1;
73 }
74 __setup("hlt", hlt_setup);
75 
76 void default_idle(void)
77 {
78 	if (likely(hlt_counter)) {
79 		while (!need_resched())
80 			cpu_relax();
81 	} else {
82 		clear_thread_flag(TIF_POLLING_NRFLAG);
83 		smp_mb__after_clear_bit();
84 		local_irq_disable();
85 		while (!need_resched())
86 			cpu_sleep();
87 		local_irq_enable();
88 		set_thread_flag(TIF_POLLING_NRFLAG);
89 	}
90 }
91 
92 void cpu_idle(void)
93 {
94 	set_thread_flag(TIF_POLLING_NRFLAG);
95 
96 	/* endless idle loop with no priority at all */
97 	while (1) {
98 		void (*idle)(void) = pm_idle;
99 
100 		if (!idle)
101 			idle = default_idle;
102 
103 		tick_nohz_stop_sched_tick(1);
104 		while (!need_resched())
105 			idle();
106 		tick_nohz_restart_sched_tick();
107 
108 		preempt_enable_no_resched();
109 		schedule();
110 		preempt_disable();
111 		check_pgt_cache();
112 	}
113 }
114 
115 void flush_thread(void)
116 {
117 }
118 
119 int copy_thread(unsigned long clone_flags, unsigned long usp,
120 		unsigned long unused,
121 		struct task_struct *p, struct pt_regs *regs)
122 {
123 	struct pt_regs *childregs = task_pt_regs(p);
124 	struct thread_info *ti = task_thread_info(p);
125 
126 	*childregs = *regs;
127 	if (user_mode(regs))
128 		childregs->r1 = usp;
129 	else
130 		childregs->r1 = ((unsigned long) ti) + THREAD_SIZE;
131 
132 #ifndef CONFIG_MMU
133 	memset(&ti->cpu_context, 0, sizeof(struct cpu_context));
134 	ti->cpu_context.r1 = (unsigned long)childregs;
135 	ti->cpu_context.msr = (unsigned long)childregs->msr;
136 #else
137 
138 	/* if creating a kernel thread then update the current reg (we don't
139 	 * want to use the parent's value when restoring by POP_STATE) */
140 	if (kernel_mode(regs))
141 		/* save new current on stack to use POP_STATE */
142 		childregs->CURRENT_TASK = (unsigned long)p;
143 	/* if returning to user then use the parent's value of this register */
144 
145 	/* if we're creating a new kernel thread then just zeroing all
146 	 * the registers. That's OK for a brand new thread.*/
147 	/* Pls. note that some of them will be restored in POP_STATE */
148 	if (kernel_mode(regs))
149 		memset(&ti->cpu_context, 0, sizeof(struct cpu_context));
150 	/* if this thread is created for fork/vfork/clone, then we want to
151 	 * restore all the parent's context */
152 	/* in addition to the registers which will be restored by POP_STATE */
153 	else {
154 		ti->cpu_context = *(struct cpu_context *)regs;
155 		childregs->msr |= MSR_UMS;
156 	}
157 
158 	/* FIXME STATE_SAVE_PT_OFFSET; */
159 	ti->cpu_context.r1  = (unsigned long)childregs - STATE_SAVE_ARG_SPACE;
160 	/* we should consider the fact that childregs is a copy of the parent
161 	 * regs which were saved immediately after entering the kernel state
162 	 * before enabling VM. This MSR will be restored in switch_to and
163 	 * RETURN() and we want to have the right machine state there
164 	 * specifically this state must have INTs disabled before and enabled
165 	 * after performing rtbd
166 	 * compose the right MSR for RETURN(). It will work for switch_to also
167 	 * excepting for VM and UMS
168 	 * don't touch UMS , CARRY and cache bits
169 	 * right now MSR is a copy of parent one */
170 	childregs->msr |= MSR_BIP;
171 	childregs->msr &= ~MSR_EIP;
172 	childregs->msr |= MSR_IE;
173 	childregs->msr &= ~MSR_VM;
174 	childregs->msr |= MSR_VMS;
175 	childregs->msr |= MSR_EE; /* exceptions will be enabled*/
176 
177 	ti->cpu_context.msr = (childregs->msr|MSR_VM);
178 	ti->cpu_context.msr &= ~MSR_UMS; /* switch_to to kernel mode */
179 #endif
180 	ti->cpu_context.r15 = (unsigned long)ret_from_fork - 8;
181 
182 	if (clone_flags & CLONE_SETTLS)
183 		;
184 
185 	return 0;
186 }
187 
188 #ifndef CONFIG_MMU
189 /*
190  * Return saved PC of a blocked thread.
191  */
192 unsigned long thread_saved_pc(struct task_struct *tsk)
193 {
194 	struct cpu_context *ctx =
195 		&(((struct thread_info *)(tsk->stack))->cpu_context);
196 
197 	/* Check whether the thread is blocked in resume() */
198 	if (in_sched_functions(ctx->r15))
199 		return (unsigned long)ctx->r15;
200 	else
201 		return ctx->r14;
202 }
203 #endif
204 
205 static void kernel_thread_helper(int (*fn)(void *), void *arg)
206 {
207 	fn(arg);
208 	do_exit(-1);
209 }
210 
211 int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
212 {
213 	struct pt_regs regs;
214 
215 	memset(&regs, 0, sizeof(regs));
216 	/* store them in non-volatile registers */
217 	regs.r5 = (unsigned long)fn;
218 	regs.r6 = (unsigned long)arg;
219 	local_save_flags(regs.msr);
220 	regs.pc = (unsigned long)kernel_thread_helper;
221 	regs.pt_mode = 1;
222 
223 	return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0,
224 			&regs, 0, NULL, NULL);
225 }
226 EXPORT_SYMBOL_GPL(kernel_thread);
227 
228 unsigned long get_wchan(struct task_struct *p)
229 {
230 /* TBD (used by procfs) */
231 	return 0;
232 }
233 
234 /* Set up a thread for executing a new program */
235 void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long usp)
236 {
237 	set_fs(USER_DS);
238 	regs->pc = pc;
239 	regs->r1 = usp;
240 	regs->pt_mode = 0;
241 #ifdef CONFIG_MMU
242 	regs->msr |= MSR_UMS;
243 #endif
244 }
245 
246 #ifdef CONFIG_MMU
247 #include <linux/elfcore.h>
248 /*
249  * Set up a thread for executing a new program
250  */
251 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
252 {
253 	return 0; /* MicroBlaze has no separate FPU registers */
254 }
255 #endif /* CONFIG_MMU */
256