[PATCH] add missing pm_power_off's
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / xtensa / kernel / process.c
blob64a649eb883f2d4261ade0f4465549bad64d00fc
1 // TODO verify coprocessor handling
2 /*
3 * arch/xtensa/kernel/process.c
5 * Xtensa Processor version.
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
9 * for more details.
11 * Copyright (C) 2001 - 2005 Tensilica Inc.
13 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
14 * Chris Zankel <chris@zankel.net>
15 * Marc Gauthier <marc@tensilica.com, marc@alumni.uwaterloo.ca>
16 * Kevin Chea
19 #include <linux/config.h>
20 #include <linux/errno.h>
21 #include <linux/sched.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/smp.h>
25 #include <linux/smp_lock.h>
26 #include <linux/stddef.h>
27 #include <linux/unistd.h>
28 #include <linux/ptrace.h>
29 #include <linux/slab.h>
30 #include <linux/elf.h>
31 #include <linux/init.h>
32 #include <linux/prctl.h>
33 #include <linux/init_task.h>
34 #include <linux/module.h>
35 #include <linux/mqueue.h>
37 #include <asm/pgtable.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
40 #include <asm/io.h>
41 #include <asm/processor.h>
42 #include <asm/platform.h>
43 #include <asm/mmu.h>
44 #include <asm/irq.h>
45 #include <asm/atomic.h>
46 #include <asm/asm-offsets.h>
47 #include <asm/coprocessor.h>
49 extern void ret_from_fork(void);
51 static struct fs_struct init_fs = INIT_FS;
52 static struct files_struct init_files = INIT_FILES;
53 static struct signal_struct init_signals = INIT_SIGNALS(init_signals);
54 static struct sighand_struct init_sighand = INIT_SIGHAND(init_sighand);
55 struct mm_struct init_mm = INIT_MM(init_mm);
56 EXPORT_SYMBOL(init_mm);
58 union thread_union init_thread_union
59 __attribute__((__section__(".data.init_task"))) =
60 { INIT_THREAD_INFO(init_task) };
62 struct task_struct init_task = INIT_TASK(init_task);
63 EXPORT_SYMBOL(init_task);
65 struct task_struct *current_set[NR_CPUS] = {&init_task, };
67 void (*pm_power_off)(void) = NULL;
68 EXPORT_SYMBOL(pm_power_off);
71 #if XCHAL_CP_NUM > 0
74 * Coprocessor ownership.
77 coprocessor_info_t coprocessor_info[] = {
78 { 0, XTENSA_CPE_CP0_OFFSET },
79 { 0, XTENSA_CPE_CP1_OFFSET },
80 { 0, XTENSA_CPE_CP2_OFFSET },
81 { 0, XTENSA_CPE_CP3_OFFSET },
82 { 0, XTENSA_CPE_CP4_OFFSET },
83 { 0, XTENSA_CPE_CP5_OFFSET },
84 { 0, XTENSA_CPE_CP6_OFFSET },
85 { 0, XTENSA_CPE_CP7_OFFSET },
88 #endif
91 * Powermanagement idle function, if any is provided by the platform.
94 void cpu_idle(void)
96 local_irq_enable();
98 /* endless idle loop with no priority at all */
99 while (1) {
100 while (!need_resched())
101 platform_idle();
102 preempt_enable_no_resched();
103 schedule();
104 preempt_disable();
109 * Free current thread data structures etc..
112 void exit_thread(void)
114 release_coprocessors(current); /* Empty macro if no CPs are defined */
117 void flush_thread(void)
119 release_coprocessors(current); /* Empty macro if no CPs are defined */
123 * Copy thread.
125 * The stack layout for the new thread looks like this:
127 * +------------------------+ <- sp in childregs (= tos)
128 * | childregs |
129 * +------------------------+ <- thread.sp = sp in dummy-frame
130 * | dummy-frame | (saved in dummy-frame spill-area)
131 * +------------------------+
133 * We create a dummy frame to return to ret_from_fork:
134 * a0 points to ret_from_fork (simulating a call4)
135 * sp points to itself (thread.sp)
136 * a2, a3 are unused.
138 * Note: This is a pristine frame, so we don't need any spill region on top of
139 * childregs.
142 int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
143 unsigned long unused,
144 struct task_struct * p, struct pt_regs * regs)
146 struct pt_regs *childregs;
147 unsigned long tos;
148 int user_mode = user_mode(regs);
150 /* Set up new TSS. */
151 tos = (unsigned long)task_stack_page(p) + THREAD_SIZE;
152 if (user_mode)
153 childregs = (struct pt_regs*)(tos - PT_USER_SIZE);
154 else
155 childregs = (struct pt_regs*)tos - 1;
157 *childregs = *regs;
159 /* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
160 *((int*)childregs - 3) = (unsigned long)childregs;
161 *((int*)childregs - 4) = 0;
163 childregs->areg[1] = tos;
164 childregs->areg[2] = 0;
165 p->set_child_tid = p->clear_child_tid = NULL;
166 p->thread.ra = MAKE_RA_FOR_CALL((unsigned long)ret_from_fork, 0x1);
167 p->thread.sp = (unsigned long)childregs;
168 if (user_mode(regs)) {
170 int len = childregs->wmask & ~0xf;
171 childregs->areg[1] = usp;
172 memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
173 &regs->areg[XCHAL_NUM_AREGS - len/4], len);
175 if (clone_flags & CLONE_SETTLS)
176 childregs->areg[2] = childregs->areg[6];
178 } else {
179 /* In kernel space, we start a new thread with a new stack. */
180 childregs->wmask = 1;
182 return 0;
187 * Create a kernel thread
190 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
192 long retval;
193 __asm__ __volatile__
194 ("mov a5, %4\n\t" /* preserve fn in a5 */
195 "mov a6, %3\n\t" /* preserve and setup arg in a6 */
196 "movi a2, %1\n\t" /* load __NR_clone for syscall*/
197 "mov a3, sp\n\t" /* sp check and sys_clone */
198 "mov a4, %5\n\t" /* load flags for syscall */
199 "syscall\n\t"
200 "beq a3, sp, 1f\n\t" /* branch if parent */
201 "callx4 a5\n\t" /* call fn */
202 "movi a2, %2\n\t" /* load __NR_exit for syscall */
203 "mov a3, a6\n\t" /* load fn return value */
204 "syscall\n"
205 "1:\n\t"
206 "mov %0, a2\n\t" /* parent returns zero */
207 :"=r" (retval)
208 :"i" (__NR_clone), "i" (__NR_exit),
209 "r" (arg), "r" (fn),
210 "r" (flags | CLONE_VM)
211 : "a2", "a3", "a4", "a5", "a6" );
212 return retval;
217 * These bracket the sleeping functions..
220 unsigned long get_wchan(struct task_struct *p)
222 unsigned long sp, pc;
223 unsigned long stack_page = (unsigned long) task_stack_page(p);
224 int count = 0;
226 if (!p || p == current || p->state == TASK_RUNNING)
227 return 0;
229 sp = p->thread.sp;
230 pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);
232 do {
233 if (sp < stack_page + sizeof(struct task_struct) ||
234 sp >= (stack_page + THREAD_SIZE) ||
235 pc == 0)
236 return 0;
237 if (!in_sched_functions(pc))
238 return pc;
240 /* Stack layout: sp-4: ra, sp-3: sp' */
242 pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
243 sp = *(unsigned long *)sp - 3;
244 } while (count++ < 16);
245 return 0;
249 * do_copy_regs() gathers information from 'struct pt_regs' and
250 * 'current->thread.areg[]' to fill in the xtensa_gregset_t
251 * structure.
253 * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
254 * of processor registers. Besides different ordering,
255 * xtensa_gregset_t contains non-live register information that
256 * 'struct pt_regs' does not. Exception handling (primarily) uses
257 * 'struct pt_regs'. Core files and ptrace use xtensa_gregset_t.
261 void do_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs,
262 struct task_struct *tsk)
264 int i, n, wb_offset;
266 elfregs->xchal_config_id0 = XCHAL_HW_CONFIGID0;
267 elfregs->xchal_config_id1 = XCHAL_HW_CONFIGID1;
269 __asm__ __volatile__ ("rsr %0, 176\n" : "=a" (i));
270 elfregs->cpux = i;
271 __asm__ __volatile__ ("rsr %0, 208\n" : "=a" (i));
272 elfregs->cpuy = i;
274 /* Note: PS.EXCM is not set while user task is running; its
275 * being set in regs->ps is for exception handling convenience.
278 elfregs->pc = regs->pc;
279 elfregs->ps = (regs->ps & ~XCHAL_PS_EXCM_MASK);
280 elfregs->exccause = regs->exccause;
281 elfregs->excvaddr = regs->excvaddr;
282 elfregs->windowbase = regs->windowbase;
283 elfregs->windowstart = regs->windowstart;
284 elfregs->lbeg = regs->lbeg;
285 elfregs->lend = regs->lend;
286 elfregs->lcount = regs->lcount;
287 elfregs->sar = regs->sar;
288 elfregs->syscall = regs->syscall;
290 /* Copy register file.
291 * The layout looks like this:
293 * | a0 ... a15 | Z ... Z | arX ... arY |
294 * current window unused saved frames
297 memset (elfregs->ar, 0, sizeof(elfregs->ar));
299 wb_offset = regs->windowbase * 4;
300 n = (regs->wmask&1)? 4 : (regs->wmask&2)? 8 : (regs->wmask&4)? 12 : 16;
302 for (i = 0; i < n; i++)
303 elfregs->ar[(wb_offset + i) % XCHAL_NUM_AREGS] = regs->areg[i];
305 n = (regs->wmask >> 4) * 4;
307 for (i = XCHAL_NUM_AREGS - n; n > 0; i++, n--)
308 elfregs->ar[(wb_offset + i) % XCHAL_NUM_AREGS] = regs->areg[i];
311 void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
313 do_copy_regs ((xtensa_gregset_t *)elfregs, regs, current);
317 /* The inverse of do_copy_regs(). No error or sanity checking. */
319 void do_restore_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs,
320 struct task_struct *tsk)
322 int i, n, wb_offset;
324 /* Note: PS.EXCM is not set while user task is running; it
325 * needs to be set in regs->ps is for exception handling convenience.
328 regs->pc = elfregs->pc;
329 regs->ps = (elfregs->ps | XCHAL_PS_EXCM_MASK);
330 regs->exccause = elfregs->exccause;
331 regs->excvaddr = elfregs->excvaddr;
332 regs->windowbase = elfregs->windowbase;
333 regs->windowstart = elfregs->windowstart;
334 regs->lbeg = elfregs->lbeg;
335 regs->lend = elfregs->lend;
336 regs->lcount = elfregs->lcount;
337 regs->sar = elfregs->sar;
338 regs->syscall = elfregs->syscall;
340 /* Clear everything. */
342 memset (regs->areg, 0, sizeof(regs->areg));
344 /* Copy regs from live window frame. */
346 wb_offset = regs->windowbase * 4;
347 n = (regs->wmask&1)? 4 : (regs->wmask&2)? 8 : (regs->wmask&4)? 12 : 16;
349 for (i = 0; i < n; i++)
350 regs->areg[(wb_offset+i) % XCHAL_NUM_AREGS] = elfregs->ar[i];
352 n = (regs->wmask >> 4) * 4;
354 for (i = XCHAL_NUM_AREGS - n; n > 0; i++, n--)
355 regs->areg[(wb_offset+i) % XCHAL_NUM_AREGS] = elfregs->ar[i];
359 * do_save_fpregs() gathers information from 'struct pt_regs' and
360 * 'current->thread' to fill in the elf_fpregset_t structure.
362 * Core files and ptrace use elf_fpregset_t.
365 void do_save_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
366 struct task_struct *tsk)
368 #if XCHAL_HAVE_CP
370 extern unsigned char _xtensa_reginfo_tables[];
371 extern unsigned _xtensa_reginfo_table_size;
372 int i;
373 unsigned long flags;
375 /* Before dumping coprocessor state from memory,
376 * ensure any live coprocessor contents for this
377 * task are first saved to memory:
379 local_irq_save(flags);
381 for (i = 0; i < XCHAL_CP_MAX; i++) {
382 if (tsk == coprocessor_info[i].owner) {
383 enable_coprocessor(i);
384 save_coprocessor_registers(
385 tsk->thread.cp_save+coprocessor_info[i].offset,i);
386 disable_coprocessor(i);
390 local_irq_restore(flags);
392 /* Now dump coprocessor & extra state: */
393 memcpy((unsigned char*)fpregs,
394 _xtensa_reginfo_tables, _xtensa_reginfo_table_size);
395 memcpy((unsigned char*)fpregs + _xtensa_reginfo_table_size,
396 tsk->thread.cp_save, XTENSA_CP_EXTRA_SIZE);
397 #endif
401 * The inverse of do_save_fpregs().
402 * Copies coprocessor and extra state from fpregs into regs and tsk->thread.
403 * Returns 0 on success, non-zero if layout doesn't match.
406 int do_restore_fpregs (elf_fpregset_t *fpregs, struct pt_regs *regs,
407 struct task_struct *tsk)
409 #if XCHAL_HAVE_CP
411 extern unsigned char _xtensa_reginfo_tables[];
412 extern unsigned _xtensa_reginfo_table_size;
413 int i;
414 unsigned long flags;
416 /* Make sure save area layouts match.
417 * FIXME: in the future we could allow restoring from
418 * a different layout of the same registers, by comparing
419 * fpregs' table with _xtensa_reginfo_tables and matching
420 * entries and copying registers one at a time.
421 * Not too sure yet whether that's very useful.
424 if( memcmp((unsigned char*)fpregs,
425 _xtensa_reginfo_tables, _xtensa_reginfo_table_size) ) {
426 return -1;
429 /* Before restoring coprocessor state from memory,
430 * ensure any live coprocessor contents for this
431 * task are first invalidated.
434 local_irq_save(flags);
436 for (i = 0; i < XCHAL_CP_MAX; i++) {
437 if (tsk == coprocessor_info[i].owner) {
438 enable_coprocessor(i);
439 save_coprocessor_registers(
440 tsk->thread.cp_save+coprocessor_info[i].offset,i);
441 coprocessor_info[i].owner = 0;
442 disable_coprocessor(i);
446 local_irq_restore(flags);
448 /* Now restore coprocessor & extra state: */
450 memcpy(tsk->thread.cp_save,
451 (unsigned char*)fpregs + _xtensa_reginfo_table_size,
452 XTENSA_CP_EXTRA_SIZE);
453 #endif
454 return 0;
457 * Fill in the CP structure for a core dump for a particular task.
461 dump_task_fpu(struct pt_regs *regs, struct task_struct *task, elf_fpregset_t *r)
463 /* see asm/coprocessor.h for this magic number 16 */
464 #if XTENSA_CP_EXTRA_SIZE > 16
465 do_save_fpregs (r, regs, task);
467 /* For now, bit 16 means some extra state may be present: */
468 // FIXME!! need to track to return more accurate mask
469 return 0x10000 | XCHAL_CP_MASK;
470 #else
471 return 0; /* no coprocessors active on this processor */
472 #endif
476 * Fill in the CP structure for a core dump.
477 * This includes any FPU coprocessor.
478 * Here, we dump all coprocessors, and other ("extra") custom state.
480 * This function is called by elf_core_dump() in fs/binfmt_elf.c
481 * (in which case 'regs' comes from calls to do_coredump, see signals.c).
483 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
485 return dump_task_fpu(regs, current, r);