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/sched/debug.h>
16 #include <linux/tick.h>
18 #include <linux/err.h>
20 #include <asm/blackfin.h>
21 #include <asm/fixed_code.h>
22 #include <asm/mem_map.h>
25 asmlinkage void ret_from_fork(void);
27 /* Points to the SDRAM backup memory for the stack that is currently in
28 * L1 scratchpad memory.
30 void *current_l1_stack_save;
32 /* The number of tasks currently using a L1 stack area. The SRAM is
33 * allocated/deallocated whenever this changes from/to zero.
37 /* Start and length of the area in L1 scratchpad memory which we've allocated
41 unsigned long l1_stack_len;
43 void (*pm_power_off)(void) = NULL;
44 EXPORT_SYMBOL(pm_power_off);
47 * The idle loop on BFIN
50 void arch_cpu_idle(void)__attribute__((l1_text));
54 * This is our default idle handler. We need to disable
55 * interrupts here to ensure we don't miss a wakeup call.
57 void arch_cpu_idle(void)
60 ipipe_suspend_domain();
62 hard_local_irq_disable();
64 idle_with_irq_disabled();
66 hard_local_irq_enable();
69 #ifdef CONFIG_HOTPLUG_CPU
70 void arch_cpu_idle_dead(void)
77 * Do necessary setup to start up a newly executed thread.
79 * pass the data segment into user programs if it exists,
80 * it can't hurt anything as far as I can tell
82 void start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
86 regs->p5 = current->mm->start_data;
88 task_thread_info(current)->l1_task_info.stack_start =
89 (void *)current->mm->context.stack_start;
90 task_thread_info(current)->l1_task_info.lowest_sp = (void *)new_sp;
91 memcpy(L1_SCRATCH_TASK_INFO, &task_thread_info(current)->l1_task_info,
92 sizeof(*L1_SCRATCH_TASK_INFO));
96 EXPORT_SYMBOL_GPL(start_thread);
98 void flush_thread(void)
102 asmlinkage int bfin_clone(unsigned long clone_flags, unsigned long newsp)
104 #ifdef __ARCH_SYNC_CORE_DCACHE
105 if (current->nr_cpus_allowed == num_possible_cpus())
106 set_cpus_allowed_ptr(current, cpumask_of(smp_processor_id()));
110 return do_fork(clone_flags, newsp, 0, NULL, NULL);
114 copy_thread(unsigned long clone_flags,
115 unsigned long usp, unsigned long topstk,
116 struct task_struct *p)
118 struct pt_regs *childregs;
121 childregs = (struct pt_regs *) (task_stack_page(p) + THREAD_SIZE) - 1;
122 v = ((unsigned long *)childregs) - 2;
123 if (unlikely(p->flags & PF_KTHREAD)) {
124 memset(childregs, 0, sizeof(struct pt_regs));
127 childregs->orig_p0 = -1;
128 childregs->ipend = 0x8000;
129 __asm__ __volatile__("%0 = syscfg;":"=da"(childregs->syscfg):);
132 *childregs = *current_pt_regs();
134 p->thread.usp = usp ? : rdusp();
138 p->thread.ksp = (unsigned long)v;
139 p->thread.pc = (unsigned long)ret_from_fork;
144 unsigned long get_wchan(struct task_struct *p)
146 unsigned long fp, pc;
147 unsigned long stack_page;
149 if (!p || p == current || p->state == TASK_RUNNING)
152 stack_page = (unsigned long)p;
155 if (fp < stack_page + sizeof(struct thread_info) ||
156 fp >= 8184 + stack_page)
158 pc = ((unsigned long *)fp)[1];
159 if (!in_sched_functions(pc))
161 fp = *(unsigned long *)fp;
163 while (count++ < 16);
167 void finish_atomic_sections (struct pt_regs *regs)
169 int __user *up0 = (int __user *)regs->p0;
173 /* not in middle of an atomic step, so resume like normal */
176 case ATOMIC_XCHG32 + 2:
177 put_user(regs->r1, up0);
180 case ATOMIC_CAS32 + 2:
181 case ATOMIC_CAS32 + 4:
182 if (regs->r0 == regs->r1)
183 case ATOMIC_CAS32 + 6:
184 put_user(regs->r2, up0);
187 case ATOMIC_ADD32 + 2:
188 regs->r0 = regs->r1 + regs->r0;
190 case ATOMIC_ADD32 + 4:
191 put_user(regs->r0, up0);
194 case ATOMIC_SUB32 + 2:
195 regs->r0 = regs->r1 - regs->r0;
197 case ATOMIC_SUB32 + 4:
198 put_user(regs->r0, up0);
201 case ATOMIC_IOR32 + 2:
202 regs->r0 = regs->r1 | regs->r0;
204 case ATOMIC_IOR32 + 4:
205 put_user(regs->r0, up0);
208 case ATOMIC_AND32 + 2:
209 regs->r0 = regs->r1 & regs->r0;
211 case ATOMIC_AND32 + 4:
212 put_user(regs->r0, up0);
215 case ATOMIC_XOR32 + 2:
216 regs->r0 = regs->r1 ^ regs->r0;
218 case ATOMIC_XOR32 + 4:
219 put_user(regs->r0, up0);
224 * We've finished the atomic section, and the only thing left for
225 * userspace is to do a RTS, so we might as well handle that too
226 * since we need to update the PC anyways.
228 regs->pc = regs->rets;
232 int in_mem(unsigned long addr, unsigned long size,
233 unsigned long start, unsigned long end)
235 return addr >= start && addr + size <= end;
238 int in_mem_const_off(unsigned long addr, unsigned long size, unsigned long off,
239 unsigned long const_addr, unsigned long const_size)
242 in_mem(addr, size, const_addr + off, const_addr + const_size);
245 int in_mem_const(unsigned long addr, unsigned long size,
246 unsigned long const_addr, unsigned long const_size)
248 return in_mem_const_off(addr, size, 0, const_addr, const_size);
251 #define ASYNC_ENABLED(bnum, bctlnum) 1
253 #define ASYNC_ENABLED(bnum, bctlnum) \
255 (bfin_read_EBIU_AMGCTL() & 0xe) < ((bnum + 1) << 1) ? 0 : \
256 bfin_read_EBIU_AMBCTL##bctlnum() & B##bnum##RDYEN ? 0 : \
261 * We can't read EBIU banks that aren't enabled or we end up hanging
262 * on the access to the async space. Make sure we validate accesses
263 * that cross async banks too.
264 * 0 - found, but unusable
269 int in_async(unsigned long addr, unsigned long size)
271 if (addr >= ASYNC_BANK0_BASE && addr < ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE) {
272 if (!ASYNC_ENABLED(0, 0))
274 if (addr + size <= ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE)
276 size -= ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE - addr;
277 addr = ASYNC_BANK0_BASE + ASYNC_BANK0_SIZE;
279 if (addr >= ASYNC_BANK1_BASE && addr < ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE) {
280 if (!ASYNC_ENABLED(1, 0))
282 if (addr + size <= ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE)
284 size -= ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE - addr;
285 addr = ASYNC_BANK1_BASE + ASYNC_BANK1_SIZE;
287 if (addr >= ASYNC_BANK2_BASE && addr < ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE) {
288 if (!ASYNC_ENABLED(2, 1))
290 if (addr + size <= ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE)
292 size -= ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE - addr;
293 addr = ASYNC_BANK2_BASE + ASYNC_BANK2_SIZE;
295 if (addr >= ASYNC_BANK3_BASE && addr < ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE) {
296 if (ASYNC_ENABLED(3, 1))
298 if (addr + size <= ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE)
303 /* not within async bounds */
307 int bfin_mem_access_type(unsigned long addr, unsigned long size)
309 int cpu = raw_smp_processor_id();
311 /* Check that things do not wrap around */
312 if (addr > ULONG_MAX - size)
315 if (in_mem(addr, size, FIXED_CODE_START, physical_mem_end))
316 return BFIN_MEM_ACCESS_CORE;
318 if (in_mem_const(addr, size, L1_CODE_START, L1_CODE_LENGTH))
319 return cpu == 0 ? BFIN_MEM_ACCESS_ITEST : BFIN_MEM_ACCESS_IDMA;
320 if (in_mem_const(addr, size, L1_SCRATCH_START, L1_SCRATCH_LENGTH))
321 return cpu == 0 ? BFIN_MEM_ACCESS_CORE_ONLY : -EFAULT;
322 if (in_mem_const(addr, size, L1_DATA_A_START, L1_DATA_A_LENGTH))
323 return cpu == 0 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
324 if (in_mem_const(addr, size, L1_DATA_B_START, L1_DATA_B_LENGTH))
325 return cpu == 0 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
326 #ifdef COREB_L1_CODE_START
327 if (in_mem_const(addr, size, COREB_L1_CODE_START, COREB_L1_CODE_LENGTH))
328 return cpu == 1 ? BFIN_MEM_ACCESS_ITEST : BFIN_MEM_ACCESS_IDMA;
329 if (in_mem_const(addr, size, COREB_L1_SCRATCH_START, L1_SCRATCH_LENGTH))
330 return cpu == 1 ? BFIN_MEM_ACCESS_CORE_ONLY : -EFAULT;
331 if (in_mem_const(addr, size, COREB_L1_DATA_A_START, COREB_L1_DATA_A_LENGTH))
332 return cpu == 1 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
333 if (in_mem_const(addr, size, COREB_L1_DATA_B_START, COREB_L1_DATA_B_LENGTH))
334 return cpu == 1 ? BFIN_MEM_ACCESS_CORE : BFIN_MEM_ACCESS_IDMA;
336 if (in_mem_const(addr, size, L2_START, L2_LENGTH))
337 return BFIN_MEM_ACCESS_CORE;
339 if (addr >= SYSMMR_BASE)
340 return BFIN_MEM_ACCESS_CORE_ONLY;
342 switch (in_async(addr, size)) {
343 case 0: return -EFAULT;
344 case 1: return BFIN_MEM_ACCESS_CORE;
345 case 2: /* fall through */;
348 if (in_mem_const(addr, size, BOOT_ROM_START, BOOT_ROM_LENGTH))
349 return BFIN_MEM_ACCESS_CORE;
350 if (in_mem_const(addr, size, L1_ROM_START, L1_ROM_LENGTH))
351 return BFIN_MEM_ACCESS_DMA;
356 #if defined(CONFIG_ACCESS_CHECK)
357 #ifdef CONFIG_ACCESS_OK_L1
358 __attribute__((l1_text))
360 /* Return 1 if access to memory range is OK, 0 otherwise */
361 int _access_ok(unsigned long addr, unsigned long size)
367 /* Check that things do not wrap around */
368 if (addr > ULONG_MAX - size)
370 if (segment_eq(get_fs(), KERNEL_DS))
372 #ifdef CONFIG_MTD_UCLINUX
378 if (in_mem(addr, size, memory_start, memory_end))
380 if (in_mem(addr, size, memory_mtd_end, physical_mem_end))
382 # ifndef CONFIG_ROMFS_ON_MTD
385 /* For XIP, allow user space to use pointers within the ROMFS. */
386 if (in_mem(addr, size, memory_mtd_start, memory_mtd_end))
389 if (in_mem(addr, size, memory_start, physical_mem_end))
393 if (in_mem(addr, size, (unsigned long)__init_begin, (unsigned long)__init_end))
396 if (in_mem_const(addr, size, L1_CODE_START, L1_CODE_LENGTH))
398 if (in_mem_const_off(addr, size, _etext_l1 - _stext_l1, L1_CODE_START, L1_CODE_LENGTH))
400 if (in_mem_const_off(addr, size, _ebss_l1 - _sdata_l1, L1_DATA_A_START, L1_DATA_A_LENGTH))
402 if (in_mem_const_off(addr, size, _ebss_b_l1 - _sdata_b_l1, L1_DATA_B_START, L1_DATA_B_LENGTH))
404 #ifdef COREB_L1_CODE_START
405 if (in_mem_const(addr, size, COREB_L1_CODE_START, COREB_L1_CODE_LENGTH))
407 if (in_mem_const(addr, size, COREB_L1_SCRATCH_START, L1_SCRATCH_LENGTH))
409 if (in_mem_const(addr, size, COREB_L1_DATA_A_START, COREB_L1_DATA_A_LENGTH))
411 if (in_mem_const(addr, size, COREB_L1_DATA_B_START, COREB_L1_DATA_B_LENGTH))
415 #ifndef CONFIG_EXCEPTION_L1_SCRATCH
416 if (in_mem_const(addr, size, (unsigned long)l1_stack_base, l1_stack_len))
420 aret = in_async(addr, size);
424 if (in_mem_const_off(addr, size, _ebss_l2 - _stext_l2, L2_START, L2_LENGTH))
427 if (in_mem_const(addr, size, BOOT_ROM_START, BOOT_ROM_LENGTH))
429 if (in_mem_const(addr, size, L1_ROM_START, L1_ROM_LENGTH))
434 EXPORT_SYMBOL(_access_ok);
435 #endif /* CONFIG_ACCESS_CHECK */