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/unistd.h>
11 #include <linux/user.h>
12 #include <linux/uaccess.h>
13 #include <linux/slab.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>
24 asmlinkage void ret_from_fork(void);
26 /* Points to the SDRAM backup memory for the stack that is currently in
27 * L1 scratchpad memory.
29 void *current_l1_stack_save;
31 /* The number of tasks currently using a L1 stack area. The SRAM is
32 * allocated/deallocated whenever this changes from/to zero.
36 /* Start and length of the area in L1 scratchpad memory which we've allocated
40 unsigned long l1_stack_len;
42 void (*pm_power_off)(void) = NULL;
43 EXPORT_SYMBOL(pm_power_off);
46 * The idle loop on BFIN
49 void arch_cpu_idle(void)__attribute__((l1_text));
53 * This is our default idle handler. We need to disable
54 * interrupts here to ensure we don't miss a wakeup call.
56 void arch_cpu_idle(void)
59 ipipe_suspend_domain();
61 hard_local_irq_disable();
63 idle_with_irq_disabled();
65 hard_local_irq_enable();
68 #ifdef CONFIG_HOTPLUG_CPU
69 void arch_cpu_idle_dead(void)
76 * Do necessary setup to start up a newly executed thread.
78 * pass the data segment into user programs if it exists,
79 * it can't hurt anything as far as I can tell
81 void start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
85 regs->p5 = current->mm->start_data;
87 task_thread_info(current)->l1_task_info.stack_start =
88 (void *)current->mm->context.stack_start;
89 task_thread_info(current)->l1_task_info.lowest_sp = (void *)new_sp;
90 memcpy(L1_SCRATCH_TASK_INFO, &task_thread_info(current)->l1_task_info,
91 sizeof(*L1_SCRATCH_TASK_INFO));
95 EXPORT_SYMBOL_GPL(start_thread);
97 void flush_thread(void)
101 asmlinkage int bfin_clone(unsigned long clone_flags, unsigned long newsp)
103 #ifdef __ARCH_SYNC_CORE_DCACHE
104 if (current->nr_cpus_allowed == num_possible_cpus())
105 set_cpus_allowed_ptr(current, cpumask_of(smp_processor_id()));
109 return do_fork(clone_flags, newsp, 0, NULL, NULL);
113 copy_thread(unsigned long clone_flags,
114 unsigned long usp, unsigned long topstk,
115 struct task_struct *p)
117 struct pt_regs *childregs;
120 childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
121 v = ((unsigned long *)childregs) - 2;
122 if (unlikely(p->flags & PF_KTHREAD)) {
123 memset(childregs, 0, sizeof(struct pt_regs));
126 childregs->orig_p0 = -1;
127 childregs->ipend = 0x8000;
128 __asm__ __volatile__("%0 = syscfg;":"=da"(childregs->syscfg):);
131 *childregs = *current_pt_regs();
133 p->thread.usp = usp ? : rdusp();
137 p->thread.ksp = (unsigned long)v;
138 p->thread.pc = (unsigned long)ret_from_fork;
143 unsigned long get_wchan(struct task_struct *p)
145 unsigned long fp, pc;
146 unsigned long stack_page;
148 if (!p || p == current || p->state == TASK_RUNNING)
151 stack_page = (unsigned long)p;
154 if (fp < stack_page + sizeof(struct thread_info) ||
155 fp >= 8184 + stack_page)
157 pc = ((unsigned long *)fp)[1];
158 if (!in_sched_functions(pc))
160 fp = *(unsigned long *)fp;
162 while (count++ < 16);
166 void finish_atomic_sections (struct pt_regs *regs)
168 int __user *up0 = (int __user *)regs->p0;
172 /* not in middle of an atomic step, so resume like normal */
175 case ATOMIC_XCHG32 + 2:
176 put_user(regs->r1, up0);
179 case ATOMIC_CAS32 + 2:
180 case ATOMIC_CAS32 + 4:
181 if (regs->r0 == regs->r1)
182 case ATOMIC_CAS32 + 6:
183 put_user(regs->r2, up0);
186 case ATOMIC_ADD32 + 2:
187 regs->r0 = regs->r1 + regs->r0;
189 case ATOMIC_ADD32 + 4:
190 put_user(regs->r0, up0);
193 case ATOMIC_SUB32 + 2:
194 regs->r0 = regs->r1 - regs->r0;
196 case ATOMIC_SUB32 + 4:
197 put_user(regs->r0, up0);
200 case ATOMIC_IOR32 + 2:
201 regs->r0 = regs->r1 | regs->r0;
203 case ATOMIC_IOR32 + 4:
204 put_user(regs->r0, up0);
207 case ATOMIC_AND32 + 2:
208 regs->r0 = regs->r1 & regs->r0;
210 case ATOMIC_AND32 + 4:
211 put_user(regs->r0, up0);
214 case ATOMIC_XOR32 + 2:
215 regs->r0 = regs->r1 ^ regs->r0;
217 case ATOMIC_XOR32 + 4:
218 put_user(regs->r0, up0);
223 * We've finished the atomic section, and the only thing left for
224 * userspace is to do a RTS, so we might as well handle that too
225 * since we need to update the PC anyways.
227 regs->pc = regs->rets;
231 int in_mem(unsigned long addr, unsigned long size,
232 unsigned long start, unsigned long end)
234 return addr >= start && addr + size <= end;
237 int in_mem_const_off(unsigned long addr, unsigned long size, unsigned long off,
238 unsigned long const_addr, unsigned long const_size)
241 in_mem(addr, size, const_addr + off, const_addr + const_size);
244 int in_mem_const(unsigned long addr, unsigned long size,
245 unsigned long const_addr, unsigned long const_size)
247 return in_mem_const_off(addr, size, 0, const_addr, const_size);
250 #define ASYNC_ENABLED(bnum, bctlnum) 1
252 #define ASYNC_ENABLED(bnum, bctlnum) \
254 (bfin_read_EBIU_AMGCTL() & 0xe) < ((bnum + 1) << 1) ? 0 : \
255 bfin_read_EBIU_AMBCTL##bctlnum() & B##bnum##RDYEN ? 0 : \
260 * We can't read EBIU banks that aren't enabled or we end up hanging
261 * on the access to the async space. Make sure we validate accesses
262 * that cross async banks too.
263 * 0 - found, but unusable
268 int in_async(unsigned long addr, unsigned long size)
270 if (addr >= ASYNC_BANK0_BASE && addr < ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE) {
271 if (!ASYNC_ENABLED(0, 0))
273 if (addr + size <= ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE)
275 size -= ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE - addr;
276 addr = ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE;
278 if (addr >= ASYNC_BANK1_BASE && addr < ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE) {
279 if (!ASYNC_ENABLED(1, 0))
281 if (addr + size <= ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE)
283 size -= ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE - addr;
284 addr = ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE;
286 if (addr >= ASYNC_BANK2_BASE && addr < ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE) {
287 if (!ASYNC_ENABLED(2, 1))
289 if (addr + size <= ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE)
291 size -= ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE - addr;
292 addr = ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE;
294 if (addr >= ASYNC_BANK3_BASE && addr < ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE) {
295 if (ASYNC_ENABLED(3, 1))
297 if (addr + size <= ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE)
302 /* not within async bounds */
306 int bfin_mem_access_type(unsigned long addr, unsigned long size)
308 int cpu = raw_smp_processor_id();
310 /* Check that things do not wrap around */
311 if (addr > ULONG_MAX - size)
314 if (in_mem(addr, size, FIXED_CODE_START, physical_mem_end))
315 return BFIN_MEM_ACCESS_CORE;
317 if (in_mem_const(addr, size, L1_CODE_START, L1_CODE_LENGTH))
318 return cpu == 0 ? BFIN_MEM_ACCESS_ITEST : BFIN_MEM_ACCESS_IDMA;
319 if (in_mem_const(addr, size, L1_SCRATCH_START, L1_SCRATCH_LENGTH))
320 return cpu == 0 ? BFIN_MEM_ACCESS_CORE_ONLY : -EFAULT;
321 if (in_mem_const(addr, size, L1_DATA_A_START, L1_DATA_A_LENGTH))
322 return cpu == 0 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
323 if (in_mem_const(addr, size, L1_DATA_B_START, L1_DATA_B_LENGTH))
324 return cpu == 0 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
325 #ifdef COREB_L1_CODE_START
326 if (in_mem_const(addr, size, COREB_L1_CODE_START, COREB_L1_CODE_LENGTH))
327 return cpu == 1 ? BFIN_MEM_ACCESS_ITEST : BFIN_MEM_ACCESS_IDMA;
328 if (in_mem_const(addr, size, COREB_L1_SCRATCH_START, L1_SCRATCH_LENGTH))
329 return cpu == 1 ? BFIN_MEM_ACCESS_CORE_ONLY : -EFAULT;
330 if (in_mem_const(addr, size, COREB_L1_DATA_A_START, COREB_L1_DATA_A_LENGTH))
331 return cpu == 1 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
332 if (in_mem_const(addr, size, COREB_L1_DATA_B_START, COREB_L1_DATA_B_LENGTH))
333 return cpu == 1 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
335 if (in_mem_const(addr, size, L2_START, L2_LENGTH))
336 return BFIN_MEM_ACCESS_CORE;
338 if (addr >= SYSMMR_BASE)
339 return BFIN_MEM_ACCESS_CORE_ONLY;
341 switch (in_async(addr, size)) {
342 case 0: return -EFAULT;
343 case 1: return BFIN_MEM_ACCESS_CORE;
344 case 2: /* fall through */;
347 if (in_mem_const(addr, size, BOOT_ROM_START, BOOT_ROM_LENGTH))
348 return BFIN_MEM_ACCESS_CORE;
349 if (in_mem_const(addr, size, L1_ROM_START, L1_ROM_LENGTH))
350 return BFIN_MEM_ACCESS_DMA;
355 #if defined(CONFIG_ACCESS_CHECK)
356 #ifdef CONFIG_ACCESS_OK_L1
357 __attribute__((l1_text))
359 /* Return 1 if access to memory range is OK, 0 otherwise */
360 int _access_ok(unsigned long addr, unsigned long size)
366 /* Check that things do not wrap around */
367 if (addr > ULONG_MAX - size)
369 if (segment_eq(get_fs(), KERNEL_DS))
371 #ifdef CONFIG_MTD_UCLINUX
377 if (in_mem(addr, size, memory_start, memory_end))
379 if (in_mem(addr, size, memory_mtd_end, physical_mem_end))
381 # ifndef CONFIG_ROMFS_ON_MTD
384 /* For XIP, allow user space to use pointers within the ROMFS. */
385 if (in_mem(addr, size, memory_mtd_start, memory_mtd_end))
388 if (in_mem(addr, size, memory_start, physical_mem_end))
392 if (in_mem(addr, size, (unsigned long)__init_begin, (unsigned long)__init_end))
395 if (in_mem_const(addr, size, L1_CODE_START, L1_CODE_LENGTH))
397 if (in_mem_const_off(addr, size, _etext_l1 - _stext_l1, L1_CODE_START, L1_CODE_LENGTH))
399 if (in_mem_const_off(addr, size, _ebss_l1 - _sdata_l1, L1_DATA_A_START, L1_DATA_A_LENGTH))
401 if (in_mem_const_off(addr, size, _ebss_b_l1 - _sdata_b_l1, L1_DATA_B_START, L1_DATA_B_LENGTH))
403 #ifdef COREB_L1_CODE_START
404 if (in_mem_const(addr, size, COREB_L1_CODE_START, COREB_L1_CODE_LENGTH))
406 if (in_mem_const(addr, size, COREB_L1_SCRATCH_START, L1_SCRATCH_LENGTH))
408 if (in_mem_const(addr, size, COREB_L1_DATA_A_START, COREB_L1_DATA_A_LENGTH))
410 if (in_mem_const(addr, size, COREB_L1_DATA_B_START, COREB_L1_DATA_B_LENGTH))
414 #ifndef CONFIG_EXCEPTION_L1_SCRATCH
415 if (in_mem_const(addr, size, (unsigned long)l1_stack_base, l1_stack_len))
419 aret = in_async(addr, size);
423 if (in_mem_const_off(addr, size, _ebss_l2 - _stext_l2, L2_START, L2_LENGTH))
426 if (in_mem_const(addr, size, BOOT_ROM_START, BOOT_ROM_LENGTH))
428 if (in_mem_const(addr, size, L1_ROM_START, L1_ROM_LENGTH))
433 EXPORT_SYMBOL(_access_ok);
434 #endif /* CONFIG_ACCESS_CHECK */