2 * File: arch/blackfin/kernel/process.c
7 * Description: Blackfin architecture-dependent process handling.
10 * Copyright 2004-2006 Analog Devices Inc.
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <linux/module.h>
31 #include <linux/smp_lock.h>
32 #include <linux/unistd.h>
33 #include <linux/user.h>
34 #include <linux/uaccess.h>
35 #include <linux/sched.h>
36 #include <linux/tick.h>
38 #include <linux/err.h>
40 #include <asm/blackfin.h>
41 #include <asm/fixed_code.h>
42 #include <asm/mem_map.h>
44 asmlinkage void ret_from_fork(void);
46 /* Points to the SDRAM backup memory for the stack that is currently in
47 * L1 scratchpad memory.
49 void *current_l1_stack_save;
51 /* The number of tasks currently using a L1 stack area. The SRAM is
52 * allocated/deallocated whenever this changes from/to zero.
56 /* Start and length of the area in L1 scratchpad memory which we've allocated
60 unsigned long l1_stack_len;
63 * Powermanagement idle function, if any..
65 void (*pm_idle)(void) = NULL;
66 EXPORT_SYMBOL(pm_idle);
68 void (*pm_power_off)(void) = NULL;
69 EXPORT_SYMBOL(pm_power_off);
72 * The idle loop on BFIN
75 static void default_idle(void)__attribute__((l1_text));
76 void cpu_idle(void)__attribute__((l1_text));
80 * This is our default idle handler. We need to disable
81 * interrupts here to ensure we don't miss a wakeup call.
83 static void default_idle(void)
86 ipipe_suspend_domain();
88 local_irq_disable_hw();
90 idle_with_irq_disabled();
92 local_irq_enable_hw();
96 * The idle thread. We try to conserve power, while trying to keep
97 * overall latency low. The architecture specific idle is passed
98 * a value to indicate the level of "idleness" of the system.
102 /* endless idle loop with no priority at all */
104 void (*idle)(void) = pm_idle;
106 #ifdef CONFIG_HOTPLUG_CPU
107 if (cpu_is_offline(smp_processor_id()))
112 tick_nohz_stop_sched_tick(1);
113 while (!need_resched())
115 tick_nohz_restart_sched_tick();
116 preempt_enable_no_resched();
122 /* Fill in the fpu structure for a core dump. */
124 int dump_fpu(struct pt_regs *regs, elf_fpregset_t * fpregs)
130 * This gets run with P1 containing the
131 * function to call, and R1 containing
132 * the "args". Note P0 is clobbered on the way here.
134 void kernel_thread_helper(void);
135 __asm__(".section .text\n"
137 "_kernel_thread_helper:\n\t"
139 "\tr0 = r1;\n\t" "\tcall (p1);\n\t" "\tcall _do_exit;\n" ".previous");
142 * Create a kernel thread.
144 pid_t kernel_thread(int (*fn) (void *), void *arg, unsigned long flags)
148 memset(®s, 0, sizeof(regs));
150 regs.r1 = (unsigned long)arg;
151 regs.p1 = (unsigned long)fn;
152 regs.pc = (unsigned long)kernel_thread_helper;
154 /* Set bit 2 to tell ret_from_fork we should be returning to kernel
157 __asm__ __volatile__("%0 = syscfg;":"=da"(regs.syscfg):);
158 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL,
161 EXPORT_SYMBOL(kernel_thread);
164 * Do necessary setup to start up a newly executed thread.
166 * pass the data segment into user programs if it exists,
167 * it can't hurt anything as far as I can tell
169 void start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
174 regs->p5 = current->mm->start_data;
176 task_thread_info(current)->l1_task_info.stack_start =
177 (void *)current->mm->context.stack_start;
178 task_thread_info(current)->l1_task_info.lowest_sp = (void *)new_sp;
179 memcpy(L1_SCRATCH_TASK_INFO, &task_thread_info(current)->l1_task_info,
180 sizeof(*L1_SCRATCH_TASK_INFO));
184 EXPORT_SYMBOL_GPL(start_thread);
186 void flush_thread(void)
190 asmlinkage int bfin_vfork(struct pt_regs *regs)
192 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL,
196 asmlinkage int bfin_clone(struct pt_regs *regs)
198 unsigned long clone_flags;
201 #ifdef __ARCH_SYNC_CORE_DCACHE
202 if (current->rt.nr_cpus_allowed == num_possible_cpus()) {
203 current->cpus_allowed = cpumask_of_cpu(smp_processor_id());
204 current->rt.nr_cpus_allowed = 1;
208 /* syscall2 puts clone_flags in r0 and usp in r1 */
209 clone_flags = regs->r0;
215 return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
219 copy_thread(unsigned long clone_flags,
220 unsigned long usp, unsigned long topstk,
221 struct task_struct *p, struct pt_regs *regs)
223 struct pt_regs *childregs;
225 childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
230 p->thread.ksp = (unsigned long)childregs;
231 p->thread.pc = (unsigned long)ret_from_fork;
237 * sys_execve() executes a new program.
240 asmlinkage int sys_execve(char __user *name, char __user * __user *argv, char __user * __user *envp)
244 struct pt_regs *regs = (struct pt_regs *)((&name) + 6);
247 filename = getname(name);
248 error = PTR_ERR(filename);
249 if (IS_ERR(filename))
251 error = do_execve(filename, argv, envp, regs);
258 unsigned long get_wchan(struct task_struct *p)
260 unsigned long fp, pc;
261 unsigned long stack_page;
263 if (!p || p == current || p->state == TASK_RUNNING)
266 stack_page = (unsigned long)p;
269 if (fp < stack_page + sizeof(struct thread_info) ||
270 fp >= 8184 + stack_page)
272 pc = ((unsigned long *)fp)[1];
273 if (!in_sched_functions(pc))
275 fp = *(unsigned long *)fp;
277 while (count++ < 16);
281 void finish_atomic_sections (struct pt_regs *regs)
283 int __user *up0 = (int __user *)regs->p0;
286 case ATOMIC_XCHG32 + 2:
287 put_user(regs->r1, up0);
288 regs->pc = ATOMIC_XCHG32 + 4;
291 case ATOMIC_CAS32 + 2:
292 case ATOMIC_CAS32 + 4:
293 if (regs->r0 == regs->r1)
294 case ATOMIC_CAS32 + 6:
295 put_user(regs->r2, up0);
296 regs->pc = ATOMIC_CAS32 + 8;
299 case ATOMIC_ADD32 + 2:
300 regs->r0 = regs->r1 + regs->r0;
302 case ATOMIC_ADD32 + 4:
303 put_user(regs->r0, up0);
304 regs->pc = ATOMIC_ADD32 + 6;
307 case ATOMIC_SUB32 + 2:
308 regs->r0 = regs->r1 - regs->r0;
310 case ATOMIC_SUB32 + 4:
311 put_user(regs->r0, up0);
312 regs->pc = ATOMIC_SUB32 + 6;
315 case ATOMIC_IOR32 + 2:
316 regs->r0 = regs->r1 | regs->r0;
318 case ATOMIC_IOR32 + 4:
319 put_user(regs->r0, up0);
320 regs->pc = ATOMIC_IOR32 + 6;
323 case ATOMIC_AND32 + 2:
324 regs->r0 = regs->r1 & regs->r0;
326 case ATOMIC_AND32 + 4:
327 put_user(regs->r0, up0);
328 regs->pc = ATOMIC_AND32 + 6;
331 case ATOMIC_XOR32 + 2:
332 regs->r0 = regs->r1 ^ regs->r0;
334 case ATOMIC_XOR32 + 4:
335 put_user(regs->r0, up0);
336 regs->pc = ATOMIC_XOR32 + 6;
342 int in_mem(unsigned long addr, unsigned long size,
343 unsigned long start, unsigned long end)
345 return addr >= start && addr + size <= end;
348 int in_mem_const_off(unsigned long addr, unsigned long size, unsigned long off,
349 unsigned long const_addr, unsigned long const_size)
352 in_mem(addr, size, const_addr + off, const_addr + const_size);
355 int in_mem_const(unsigned long addr, unsigned long size,
356 unsigned long const_addr, unsigned long const_size)
358 return in_mem_const_off(addr, size, 0, const_addr, const_size);
360 #define IN_ASYNC(bnum, bctlnum) \
362 (bfin_read_EBIU_AMGCTL() & 0xe) < ((bnum + 1) << 1) ? -EFAULT : \
363 bfin_read_EBIU_AMBCTL##bctlnum() & B##bnum##RDYEN ? -EFAULT : \
364 BFIN_MEM_ACCESS_CORE; \
367 int bfin_mem_access_type(unsigned long addr, unsigned long size)
369 int cpu = raw_smp_processor_id();
371 /* Check that things do not wrap around */
372 if (addr > ULONG_MAX - size)
375 if (in_mem(addr, size, FIXED_CODE_START, physical_mem_end))
376 return BFIN_MEM_ACCESS_CORE;
378 if (in_mem_const(addr, size, L1_CODE_START, L1_CODE_LENGTH))
379 return cpu == 0 ? BFIN_MEM_ACCESS_ITEST : BFIN_MEM_ACCESS_IDMA;
380 if (in_mem_const(addr, size, L1_SCRATCH_START, L1_SCRATCH_LENGTH))
381 return cpu == 0 ? BFIN_MEM_ACCESS_CORE_ONLY : -EFAULT;
382 if (in_mem_const(addr, size, L1_DATA_A_START, L1_DATA_A_LENGTH))
383 return cpu == 0 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
384 if (in_mem_const(addr, size, L1_DATA_B_START, L1_DATA_B_LENGTH))
385 return cpu == 0 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
386 #ifdef COREB_L1_CODE_START
387 if (in_mem_const(addr, size, COREB_L1_CODE_START, COREB_L1_CODE_LENGTH))
388 return cpu == 1 ? BFIN_MEM_ACCESS_ITEST : BFIN_MEM_ACCESS_IDMA;
389 if (in_mem_const(addr, size, COREB_L1_SCRATCH_START, L1_SCRATCH_LENGTH))
390 return cpu == 1 ? BFIN_MEM_ACCESS_CORE_ONLY : -EFAULT;
391 if (in_mem_const(addr, size, COREB_L1_DATA_A_START, COREB_L1_DATA_A_LENGTH))
392 return cpu == 1 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
393 if (in_mem_const(addr, size, COREB_L1_DATA_B_START, COREB_L1_DATA_B_LENGTH))
394 return cpu == 1 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
396 if (in_mem_const(addr, size, L2_START, L2_LENGTH))
397 return BFIN_MEM_ACCESS_CORE;
399 if (addr >= SYSMMR_BASE)
400 return BFIN_MEM_ACCESS_CORE_ONLY;
402 /* We can't read EBIU banks that aren't enabled or we end up hanging
403 * on the access to the async space.
405 if (in_mem_const(addr, size, ASYNC_BANK0_BASE, ASYNC_BANK0_SIZE))
406 return IN_ASYNC(0, 0);
407 if (in_mem_const(addr, size, ASYNC_BANK1_BASE, ASYNC_BANK1_SIZE))
408 return IN_ASYNC(1, 0);
409 if (in_mem_const(addr, size, ASYNC_BANK2_BASE, ASYNC_BANK2_SIZE))
410 return IN_ASYNC(2, 1);
411 if (in_mem_const(addr, size, ASYNC_BANK3_BASE, ASYNC_BANK3_SIZE))
412 return IN_ASYNC(3, 1);
414 if (in_mem_const(addr, size, BOOT_ROM_START, BOOT_ROM_LENGTH))
415 return BFIN_MEM_ACCESS_CORE;
416 if (in_mem_const(addr, size, L1_ROM_START, L1_ROM_LENGTH))
417 return BFIN_MEM_ACCESS_DMA;
422 #if defined(CONFIG_ACCESS_CHECK)
423 #ifdef CONFIG_ACCESS_OK_L1
424 __attribute__((l1_text))
426 /* Return 1 if access to memory range is OK, 0 otherwise */
427 int _access_ok(unsigned long addr, unsigned long size)
431 /* Check that things do not wrap around */
432 if (addr > ULONG_MAX - size)
434 if (segment_eq(get_fs(), KERNEL_DS))
436 #ifdef CONFIG_MTD_UCLINUX
442 if (in_mem(addr, size, memory_start, memory_end))
444 if (in_mem(addr, size, memory_mtd_end, physical_mem_end))
446 # ifndef CONFIG_ROMFS_ON_MTD
449 /* For XIP, allow user space to use pointers within the ROMFS. */
450 if (in_mem(addr, size, memory_mtd_start, memory_mtd_end))
453 if (in_mem(addr, size, memory_start, physical_mem_end))
457 if (in_mem(addr, size, (unsigned long)__init_begin, (unsigned long)__init_end))
460 if (in_mem_const(addr, size, L1_CODE_START, L1_CODE_LENGTH))
462 if (in_mem_const_off(addr, size, _etext_l1 - _stext_l1, L1_CODE_START, L1_CODE_LENGTH))
464 if (in_mem_const_off(addr, size, _ebss_l1 - _sdata_l1, L1_DATA_A_START, L1_DATA_A_LENGTH))
466 if (in_mem_const_off(addr, size, _ebss_b_l1 - _sdata_b_l1, L1_DATA_B_START, L1_DATA_B_LENGTH))
468 #ifdef COREB_L1_CODE_START
469 if (in_mem_const(addr, size, COREB_L1_CODE_START, COREB_L1_CODE_LENGTH))
471 if (in_mem_const(addr, size, COREB_L1_SCRATCH_START, L1_SCRATCH_LENGTH))
473 if (in_mem_const(addr, size, COREB_L1_DATA_A_START, COREB_L1_DATA_A_LENGTH))
475 if (in_mem_const(addr, size, COREB_L1_DATA_B_START, COREB_L1_DATA_B_LENGTH))
478 if (in_mem_const_off(addr, size, _ebss_l2 - _stext_l2, L2_START, L2_LENGTH))
481 if (in_mem_const(addr, size, BOOT_ROM_START, BOOT_ROM_LENGTH))
483 if (in_mem_const(addr, size, L1_ROM_START, L1_ROM_LENGTH))
488 EXPORT_SYMBOL(_access_ok);
489 #endif /* CONFIG_ACCESS_CHECK */