2 * Blackfin architecture-dependent process handling
4 * Copyright 2004-2009 Analog Devices Inc.
6 * Licensed under the GPL-2 or later
9 #include <linux/module.h>
10 #include <linux/smp_lock.h>
11 #include <linux/unistd.h>
12 #include <linux/user.h>
13 #include <linux/uaccess.h>
14 #include <linux/sched.h>
15 #include <linux/tick.h>
17 #include <linux/err.h>
19 #include <asm/blackfin.h>
20 #include <asm/fixed_code.h>
21 #include <asm/mem_map.h>
23 asmlinkage void ret_from_fork(void);
25 /* Points to the SDRAM backup memory for the stack that is currently in
26 * L1 scratchpad memory.
28 void *current_l1_stack_save;
30 /* The number of tasks currently using a L1 stack area. The SRAM is
31 * allocated/deallocated whenever this changes from/to zero.
35 /* Start and length of the area in L1 scratchpad memory which we've allocated
39 unsigned long l1_stack_len;
42 * Powermanagement idle function, if any..
44 void (*pm_idle)(void) = NULL;
45 EXPORT_SYMBOL(pm_idle);
47 void (*pm_power_off)(void) = NULL;
48 EXPORT_SYMBOL(pm_power_off);
51 * The idle loop on BFIN
54 static void default_idle(void)__attribute__((l1_text));
55 void cpu_idle(void)__attribute__((l1_text));
59 * This is our default idle handler. We need to disable
60 * interrupts here to ensure we don't miss a wakeup call.
62 static void default_idle(void)
65 ipipe_suspend_domain();
67 local_irq_disable_hw();
69 idle_with_irq_disabled();
71 local_irq_enable_hw();
75 * The idle thread. We try to conserve power, while trying to keep
76 * overall latency low. The architecture specific idle is passed
77 * a value to indicate the level of "idleness" of the system.
81 /* endless idle loop with no priority at all */
83 void (*idle)(void) = pm_idle;
85 #ifdef CONFIG_HOTPLUG_CPU
86 if (cpu_is_offline(smp_processor_id()))
91 tick_nohz_stop_sched_tick(1);
92 while (!need_resched())
94 tick_nohz_restart_sched_tick();
95 preempt_enable_no_resched();
101 /* Fill in the fpu structure for a core dump. */
103 int dump_fpu(struct pt_regs *regs, elf_fpregset_t * fpregs)
109 * This gets run with P1 containing the
110 * function to call, and R1 containing
111 * the "args". Note P0 is clobbered on the way here.
113 void kernel_thread_helper(void);
114 __asm__(".section .text\n"
116 "_kernel_thread_helper:\n\t"
118 "\tr0 = r1;\n\t" "\tcall (p1);\n\t" "\tcall _do_exit;\n" ".previous");
121 * Create a kernel thread.
123 pid_t kernel_thread(int (*fn) (void *), void *arg, unsigned long flags)
127 memset(®s, 0, sizeof(regs));
129 regs.r1 = (unsigned long)arg;
130 regs.p1 = (unsigned long)fn;
131 regs.pc = (unsigned long)kernel_thread_helper;
133 /* Set bit 2 to tell ret_from_fork we should be returning to kernel
136 __asm__ __volatile__("%0 = syscfg;":"=da"(regs.syscfg):);
137 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL,
140 EXPORT_SYMBOL(kernel_thread);
143 * Do necessary setup to start up a newly executed thread.
145 * pass the data segment into user programs if it exists,
146 * it can't hurt anything as far as I can tell
148 void start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
153 regs->p5 = current->mm->start_data;
155 task_thread_info(current)->l1_task_info.stack_start =
156 (void *)current->mm->context.stack_start;
157 task_thread_info(current)->l1_task_info.lowest_sp = (void *)new_sp;
158 memcpy(L1_SCRATCH_TASK_INFO, &task_thread_info(current)->l1_task_info,
159 sizeof(*L1_SCRATCH_TASK_INFO));
163 EXPORT_SYMBOL_GPL(start_thread);
165 void flush_thread(void)
169 asmlinkage int bfin_vfork(struct pt_regs *regs)
171 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(), regs, 0, NULL,
175 asmlinkage int bfin_clone(struct pt_regs *regs)
177 unsigned long clone_flags;
180 #ifdef __ARCH_SYNC_CORE_DCACHE
181 if (current->rt.nr_cpus_allowed == num_possible_cpus()) {
182 current->cpus_allowed = cpumask_of_cpu(smp_processor_id());
183 current->rt.nr_cpus_allowed = 1;
187 /* syscall2 puts clone_flags in r0 and usp in r1 */
188 clone_flags = regs->r0;
194 return do_fork(clone_flags, newsp, regs, 0, NULL, NULL);
198 copy_thread(unsigned long clone_flags,
199 unsigned long usp, unsigned long topstk,
200 struct task_struct *p, struct pt_regs *regs)
202 struct pt_regs *childregs;
204 childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
209 p->thread.ksp = (unsigned long)childregs;
210 p->thread.pc = (unsigned long)ret_from_fork;
216 * sys_execve() executes a new program.
219 asmlinkage int sys_execve(char __user *name, char __user * __user *argv, char __user * __user *envp)
223 struct pt_regs *regs = (struct pt_regs *)((&name) + 6);
226 filename = getname(name);
227 error = PTR_ERR(filename);
228 if (IS_ERR(filename))
230 error = do_execve(filename, argv, envp, regs);
237 unsigned long get_wchan(struct task_struct *p)
239 unsigned long fp, pc;
240 unsigned long stack_page;
242 if (!p || p == current || p->state == TASK_RUNNING)
245 stack_page = (unsigned long)p;
248 if (fp < stack_page + sizeof(struct thread_info) ||
249 fp >= 8184 + stack_page)
251 pc = ((unsigned long *)fp)[1];
252 if (!in_sched_functions(pc))
254 fp = *(unsigned long *)fp;
256 while (count++ < 16);
260 void finish_atomic_sections (struct pt_regs *regs)
262 int __user *up0 = (int __user *)regs->p0;
265 case ATOMIC_XCHG32 + 2:
266 put_user(regs->r1, up0);
267 regs->pc = ATOMIC_XCHG32 + 4;
270 case ATOMIC_CAS32 + 2:
271 case ATOMIC_CAS32 + 4:
272 if (regs->r0 == regs->r1)
273 case ATOMIC_CAS32 + 6:
274 put_user(regs->r2, up0);
275 regs->pc = ATOMIC_CAS32 + 8;
278 case ATOMIC_ADD32 + 2:
279 regs->r0 = regs->r1 + regs->r0;
281 case ATOMIC_ADD32 + 4:
282 put_user(regs->r0, up0);
283 regs->pc = ATOMIC_ADD32 + 6;
286 case ATOMIC_SUB32 + 2:
287 regs->r0 = regs->r1 - regs->r0;
289 case ATOMIC_SUB32 + 4:
290 put_user(regs->r0, up0);
291 regs->pc = ATOMIC_SUB32 + 6;
294 case ATOMIC_IOR32 + 2:
295 regs->r0 = regs->r1 | regs->r0;
297 case ATOMIC_IOR32 + 4:
298 put_user(regs->r0, up0);
299 regs->pc = ATOMIC_IOR32 + 6;
302 case ATOMIC_AND32 + 2:
303 regs->r0 = regs->r1 & regs->r0;
305 case ATOMIC_AND32 + 4:
306 put_user(regs->r0, up0);
307 regs->pc = ATOMIC_AND32 + 6;
310 case ATOMIC_XOR32 + 2:
311 regs->r0 = regs->r1 ^ regs->r0;
313 case ATOMIC_XOR32 + 4:
314 put_user(regs->r0, up0);
315 regs->pc = ATOMIC_XOR32 + 6;
321 int in_mem(unsigned long addr, unsigned long size,
322 unsigned long start, unsigned long end)
324 return addr >= start && addr + size <= end;
327 int in_mem_const_off(unsigned long addr, unsigned long size, unsigned long off,
328 unsigned long const_addr, unsigned long const_size)
331 in_mem(addr, size, const_addr + off, const_addr + const_size);
334 int in_mem_const(unsigned long addr, unsigned long size,
335 unsigned long const_addr, unsigned long const_size)
337 return in_mem_const_off(addr, size, 0, const_addr, const_size);
339 #define IN_ASYNC(bnum, bctlnum) \
341 (bfin_read_EBIU_AMGCTL() & 0xe) < ((bnum + 1) << 1) ? -EFAULT : \
342 bfin_read_EBIU_AMBCTL##bctlnum() & B##bnum##RDYEN ? -EFAULT : \
343 BFIN_MEM_ACCESS_CORE; \
346 int bfin_mem_access_type(unsigned long addr, unsigned long size)
348 int cpu = raw_smp_processor_id();
350 /* Check that things do not wrap around */
351 if (addr > ULONG_MAX - size)
354 if (in_mem(addr, size, FIXED_CODE_START, physical_mem_end))
355 return BFIN_MEM_ACCESS_CORE;
357 if (in_mem_const(addr, size, L1_CODE_START, L1_CODE_LENGTH))
358 return cpu == 0 ? BFIN_MEM_ACCESS_ITEST : BFIN_MEM_ACCESS_IDMA;
359 if (in_mem_const(addr, size, L1_SCRATCH_START, L1_SCRATCH_LENGTH))
360 return cpu == 0 ? BFIN_MEM_ACCESS_CORE_ONLY : -EFAULT;
361 if (in_mem_const(addr, size, L1_DATA_A_START, L1_DATA_A_LENGTH))
362 return cpu == 0 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
363 if (in_mem_const(addr, size, L1_DATA_B_START, L1_DATA_B_LENGTH))
364 return cpu == 0 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
365 #ifdef COREB_L1_CODE_START
366 if (in_mem_const(addr, size, COREB_L1_CODE_START, COREB_L1_CODE_LENGTH))
367 return cpu == 1 ? BFIN_MEM_ACCESS_ITEST : BFIN_MEM_ACCESS_IDMA;
368 if (in_mem_const(addr, size, COREB_L1_SCRATCH_START, L1_SCRATCH_LENGTH))
369 return cpu == 1 ? BFIN_MEM_ACCESS_CORE_ONLY : -EFAULT;
370 if (in_mem_const(addr, size, COREB_L1_DATA_A_START, COREB_L1_DATA_A_LENGTH))
371 return cpu == 1 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
372 if (in_mem_const(addr, size, COREB_L1_DATA_B_START, COREB_L1_DATA_B_LENGTH))
373 return cpu == 1 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
375 if (in_mem_const(addr, size, L2_START, L2_LENGTH))
376 return BFIN_MEM_ACCESS_CORE;
378 if (addr >= SYSMMR_BASE)
379 return BFIN_MEM_ACCESS_CORE_ONLY;
381 /* We can't read EBIU banks that aren't enabled or we end up hanging
382 * on the access to the async space.
384 if (in_mem_const(addr, size, ASYNC_BANK0_BASE, ASYNC_BANK0_SIZE))
385 return IN_ASYNC(0, 0);
386 if (in_mem_const(addr, size, ASYNC_BANK1_BASE, ASYNC_BANK1_SIZE))
387 return IN_ASYNC(1, 0);
388 if (in_mem_const(addr, size, ASYNC_BANK2_BASE, ASYNC_BANK2_SIZE))
389 return IN_ASYNC(2, 1);
390 if (in_mem_const(addr, size, ASYNC_BANK3_BASE, ASYNC_BANK3_SIZE))
391 return IN_ASYNC(3, 1);
393 if (in_mem_const(addr, size, BOOT_ROM_START, BOOT_ROM_LENGTH))
394 return BFIN_MEM_ACCESS_CORE;
395 if (in_mem_const(addr, size, L1_ROM_START, L1_ROM_LENGTH))
396 return BFIN_MEM_ACCESS_DMA;
401 #if defined(CONFIG_ACCESS_CHECK)
402 #ifdef CONFIG_ACCESS_OK_L1
403 __attribute__((l1_text))
405 /* Return 1 if access to memory range is OK, 0 otherwise */
406 int _access_ok(unsigned long addr, unsigned long size)
410 /* Check that things do not wrap around */
411 if (addr > ULONG_MAX - size)
413 if (segment_eq(get_fs(), KERNEL_DS))
415 #ifdef CONFIG_MTD_UCLINUX
421 if (in_mem(addr, size, memory_start, memory_end))
423 if (in_mem(addr, size, memory_mtd_end, physical_mem_end))
425 # ifndef CONFIG_ROMFS_ON_MTD
428 /* For XIP, allow user space to use pointers within the ROMFS. */
429 if (in_mem(addr, size, memory_mtd_start, memory_mtd_end))
432 if (in_mem(addr, size, memory_start, physical_mem_end))
436 if (in_mem(addr, size, (unsigned long)__init_begin, (unsigned long)__init_end))
439 if (in_mem_const(addr, size, L1_CODE_START, L1_CODE_LENGTH))
441 if (in_mem_const_off(addr, size, _etext_l1 - _stext_l1, L1_CODE_START, L1_CODE_LENGTH))
443 if (in_mem_const_off(addr, size, _ebss_l1 - _sdata_l1, L1_DATA_A_START, L1_DATA_A_LENGTH))
445 if (in_mem_const_off(addr, size, _ebss_b_l1 - _sdata_b_l1, L1_DATA_B_START, L1_DATA_B_LENGTH))
447 #ifdef COREB_L1_CODE_START
448 if (in_mem_const(addr, size, COREB_L1_CODE_START, COREB_L1_CODE_LENGTH))
450 if (in_mem_const(addr, size, COREB_L1_SCRATCH_START, L1_SCRATCH_LENGTH))
452 if (in_mem_const(addr, size, COREB_L1_DATA_A_START, COREB_L1_DATA_A_LENGTH))
454 if (in_mem_const(addr, size, COREB_L1_DATA_B_START, COREB_L1_DATA_B_LENGTH))
457 if (in_mem_const_off(addr, size, _ebss_l2 - _stext_l2, L2_START, L2_LENGTH))
460 if (in_mem_const(addr, size, BOOT_ROM_START, BOOT_ROM_LENGTH))
462 if (in_mem_const(addr, size, L1_ROM_START, L1_ROM_LENGTH))
467 EXPORT_SYMBOL(_access_ok);
468 #endif /* CONFIG_ACCESS_CHECK */