2 * arch/ubicom32/kernel/smp.c
3 * SMP implementation for Ubicom32 processors.
5 * (C) Copyright 2009, Ubicom, Inc.
6 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
7 * Copyright (C) 1999 David Mosberger-Tang <davidm@hpl.hp.com>
8 * Copyright (C) 2001,2004 Grant Grundler <grundler@parisc-linux.org>
10 * This file is part of the Ubicom32 Linux Kernel Port.
12 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
13 * it and/or modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 2 of the
15 * License, or (at your option) any later version.
17 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
18 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
19 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
20 * the GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with the Ubicom32 Linux Kernel Port. If not,
24 * see <http://www.gnu.org/licenses/>.
26 * Ubicom32 implementation derived from (with many thanks):
32 #include <linux/types.h>
33 #include <linux/spinlock.h>
34 #include <linux/slab.h>
36 #include <linux/kernel.h>
37 #include <linux/bootmem.h>
38 #include <linux/module.h>
39 #include <linux/sched.h>
40 #include <linux/init.h>
41 #include <linux/interrupt.h>
42 #include <linux/smp.h>
43 #include <linux/kernel_stat.h>
45 #include <linux/err.h>
46 #include <linux/delay.h>
47 #include <linux/bitops.h>
48 #include <linux/cpu.h>
49 #include <linux/profile.h>
50 #include <linux/delay.h>
52 #include <linux/ptrace.h>
53 #include <linux/unistd.h>
54 #include <linux/irq.h>
56 #include <asm/system.h>
57 #include <asm/atomic.h>
58 #include <asm/current.h>
59 #include <asm/tlbflush.h>
60 #include <asm/timex.h>
63 #include <asm/processor.h>
64 #include <asm/thread.h>
65 #include <asm/sections.h>
66 #include <asm/ip5000.h>
69 * Mask the debug printout for IPI because they are too verbose
70 * for regular debugging.
73 // #define DEBUG_SMP 1
74 #if !defined(DEBUG_SMP)
75 #define smp_debug(lvl, ...)
77 static unsigned int smp_debug_lvl = 50;
78 #define smp_debug(lvl, printargs...) \
79 if (lvl >= smp_debug_lvl) { \
84 #if !defined(DEBUG_SMP)
85 #define DEBUG_ASSERT(cond)
87 #define DEBUG_ASSERT(cond) \
94 * List of IPI Commands (more than one can be set at a time).
96 enum ipi_message_type {
100 IPI_CALL_FUNC_SINGLE,
106 * We maintain a hardware thread oriented view of online threads
107 * and those involved or needing IPI.
109 static volatile unsigned long smp_online_threads = 0;
110 static volatile unsigned long smp_needs_ipi = 0;
111 static volatile unsigned long smp_inside_ipi = 0;
112 static unsigned long smp_irq_affinity[NR_IRQS];
115 * What do we need to track on a per cpu/thread basis?
117 DEFINE_PER_CPU(struct cpuinfo_ubicom32, cpu_data);
120 * Each thread cpuinfo IPI information is guarded by a lock
121 * that is kept local to this file.
123 DEFINE_PER_CPU(spinlock_t, ipi_lock) = SPIN_LOCK_UNLOCKED;
126 * The IPI(s) are based on a software IRQ through the LDSR.
128 unsigned int smp_ipi_irq;
131 * Define a spinlock so that only one cpu is able to modify the
132 * smp_needs_ipi and to set/clear the IRQ at a time.
134 DEFINE_SPINLOCK(smp_ipi_lock);
137 * smp_halt_processor()
138 * Halt this hardware thread.
140 static void smp_halt_processor(void)
142 int cpuid = thread_get_self();
143 cpu_clear(smp_processor_id(), cpu_online_map);
145 printk(KERN_EMERG "cpu[%d] has halted. It is not OK to turn off power \
146 until all cpu's are off.\n", cpuid);
154 * Handle an Interprocessor Interrupt.
156 static irqreturn_t ipi_interrupt(int irq, void *dev_id)
158 int cpuid = smp_processor_id();
159 struct cpuinfo_ubicom32 *p = &per_cpu(cpu_data, cpuid);
163 * Count this now; we may make a call that never returns.
168 * We are about to process all ops. If another cpu has stated
169 * that we need an IPI, we will have already processed it. By
170 * clearing our smp_needs_ipi, and processing all ops,
171 * we reduce the number of IPI interrupts. However, this introduces
172 * the possibility that smp_needs_ipi will be clear and the soft irq
173 * will have gone off; so we need to make the get_affinity() path
174 * tolerant of spurious interrupts.
176 spin_lock(&smp_ipi_lock);
177 smp_needs_ipi &= ~(1 << p->tid);
178 spin_unlock(&smp_ipi_lock);
182 * Read the set of IPI commands we should handle.
184 spinlock_t *lock = &per_cpu(ipi_lock, cpuid);
186 ops = p->ipi_pending;
191 * If we have no IPI commands to execute, break out.
198 * Execute the set of commands in the ops word, one command
199 * at a time in no particular order. Strip of each command
203 unsigned long which = ffz(~ops);
204 ops &= ~(1 << which);
206 BUG_ON(!irqs_disabled());
209 smp_debug(100, KERN_INFO "cpu[%d]: "
215 * Reschedule callback. Everything to be
216 * done is done by the interrupt return path.
218 smp_debug(200, KERN_INFO "cpu[%d]: "
219 "IPI_RESCHEDULE\n", cpuid);
223 smp_debug(100, KERN_INFO "cpu[%d]: "
224 "IPI_CALL_FUNC\n", cpuid);
225 generic_smp_call_function_interrupt();
228 case IPI_CALL_FUNC_SINGLE:
229 smp_debug(100, KERN_INFO "cpu[%d]: "
230 "IPI_CALL_FUNC_SINGLE\n", cpuid);
231 generic_smp_call_function_single_interrupt();
235 smp_debug(100, KERN_INFO "cpu[%d]: "
236 "IPI_CPU_STOP\n", cpuid);
237 smp_halt_processor();
240 #if !defined(CONFIG_LOCAL_TIMERS)
242 smp_debug(100, KERN_INFO "cpu[%d]: "
243 "IPI_CPU_TIMER\n", cpuid);
244 #if defined(CONFIG_GENERIC_CLOCKEVENTS)
245 local_timer_interrupt();
247 update_process_times(user_mode(get_irq_regs()));
248 profile_tick(CPU_PROFILING);
254 printk(KERN_CRIT "cpu[%d]: "
255 "Unknown IPI: %lu\n", cpuid, which);
261 * Let in any pending interrupts
263 BUG_ON(!irqs_disabled());
273 * Send an Interprocessor Interrupt.
275 static void ipi_send(int cpu, enum ipi_message_type op)
277 struct cpuinfo_ubicom32 *p = &per_cpu(cpu_data, cpu);
278 spinlock_t *lock = &per_cpu(ipi_lock, cpu);
282 * We protect the setting of the ipi_pending field and ensure
283 * that the ipi delivery mechanism and interrupt are atomically
286 spin_lock_irqsave(lock, flags);
287 p->ipi_pending |= 1 << op;
288 spin_unlock_irqrestore(lock, flags);
290 spin_lock_irqsave(&smp_ipi_lock, flags);
291 smp_needs_ipi |= (1 << p->tid);
292 ubicom32_set_interrupt(smp_ipi_irq);
293 spin_unlock_irqrestore(&smp_ipi_lock, flags);
294 smp_debug(100, KERN_INFO "cpu[%d]: send: %d\n", cpu, op);
299 * Send an IPI to each cpu in mask.
301 static inline void ipi_send_mask(unsigned int op, const struct cpumask mask)
304 for_each_cpu_mask(cpu, mask) {
310 * ipi_send_allbutself()
311 * Send an IPI to all threads but ourselves.
313 static inline void ipi_send_allbutself(unsigned int op)
315 int self = smp_processor_id();
316 struct cpumask result;
317 cpumask_copy(&result, &cpu_online_map);
318 cpu_clear(self, result);
319 ipi_send_mask(op, result);
323 * smp_enable_vector()
325 static void smp_enable_vector(unsigned int irq)
327 ubicom32_clear_interrupt(smp_ipi_irq);
328 ldsr_enable_vector(irq);
332 * smp_disable_vector()
333 * Disable the interrupt by clearing the appropriate bit in the
334 * LDSR Mask Register.
336 static void smp_disable_vector(unsigned int irq)
338 ldsr_disable_vector(irq);
344 static void smp_mask_vector(unsigned int irq)
346 ldsr_mask_vector(irq);
350 * smp_unmask_vector()
352 static void smp_unmask_vector(unsigned int irq)
354 ldsr_unmask_vector(irq);
359 * Called once an interrupt is completed (reset the LDSR mask).
361 static void smp_end_vector(unsigned int irq)
363 struct cpuinfo_ubicom32 *p = &per_cpu(cpu_data, smp_processor_id());
364 spin_lock(&smp_ipi_lock);
365 smp_inside_ipi &= ~(1 << p->tid);
366 if (smp_inside_ipi) {
367 spin_unlock(&smp_ipi_lock);
370 spin_unlock(&smp_ipi_lock);
371 ldsr_unmask_vector(irq);
372 smp_debug(100, KERN_INFO "cpu[%d]: unamesk vector\n", smp_processor_id());
376 * Special hanlder functions for SMP.
378 static struct irq_chip ubicom32_smp_chip = {
382 .enable = smp_enable_vector,
383 .disable = smp_disable_vector,
385 .mask = smp_mask_vector,
386 .unmask = smp_unmask_vector,
387 .end = smp_end_vector,
392 * None of these cpu(s) got their IPI, turn it back on.
394 * Note: This is called by the LDSR which is not a full
395 * Linux cpu. Thus you must use the raw form of locks
396 * because lock debugging will not work on the partial
397 * cpu nature of the LDSR.
399 void smp_reset_ipi(unsigned long mask)
401 __raw_spin_lock(&smp_ipi_lock.raw_lock);
402 smp_needs_ipi |= mask;
403 smp_inside_ipi &= ~mask;
404 ubicom32_set_interrupt(smp_ipi_irq);
405 __raw_spin_unlock(&smp_ipi_lock.raw_lock);
406 smp_debug(100, KERN_INFO "smp: reset IPIs for: 0x%x\n", mask);
411 * Choose the thread affinity for this interrupt.
413 * Note: This is called by the LDSR which is not a full
414 * Linux cpu. Thus you must use the raw form of locks
415 * because lock debugging will not work on the partial
416 * cpu nature of the LDSR.
418 unsigned long smp_get_affinity(unsigned int irq, int *all)
420 unsigned long mask = 0;
423 * Most IRQ(s) are delivered in a round robin fashion.
425 if (irq != smp_ipi_irq) {
426 unsigned long result = smp_irq_affinity[irq] & smp_online_threads;
427 DEBUG_ASSERT(result);
433 * This is an IPI request. Return all cpu(s) scheduled for an IPI.
434 * We also track those cpu(s) that are going to be "receiving" IPI this
435 * round. When all CPU(s) have called smp_end_vector(),
436 * we will unmask the IPI interrupt.
438 __raw_spin_lock(&smp_ipi_lock.raw_lock);
439 ubicom32_clear_interrupt(smp_ipi_irq);
441 mask = smp_needs_ipi;
442 smp_inside_ipi |= smp_needs_ipi;
445 __raw_spin_unlock(&smp_ipi_lock.raw_lock);
452 * Set the affinity for this irq but store the value in tid(s).
454 void smp_set_affinity(unsigned int irq, const struct cpumask *dest)
457 unsigned long *paffinity = &smp_irq_affinity[irq];
460 * If none specified, all cpus are allowed.
462 if (cpus_empty(*dest)) {
463 *paffinity = 0xffffffff;
468 * Make sure to clear the old value before setting up the
472 for_each_cpu_mask(cpuid, *dest) {
473 struct cpuinfo_ubicom32 *p = &per_cpu(cpu_data, cpuid);
474 *paffinity |= (1 << p->tid);
480 * Send a stop request to all CPU but this one.
482 void smp_send_stop(void)
484 ipi_send_allbutself(IPI_CPU_STOP);
488 * smp_send_timer_all()
489 * Send all cpu(s) but this one, a request to update times.
491 void smp_send_timer_all(void)
493 ipi_send_allbutself(IPI_CPU_TIMER);
497 * smp_timer_broadcast()
498 * Use an IPI to broadcast a timer message
500 void smp_timer_broadcast(const struct cpumask *mask)
502 ipi_send_mask(IPI_CPU_TIMER, *mask);
506 * smp_send_reschedule()
507 * Send a reschedule request to the specified cpu.
509 void smp_send_reschedule(int cpu)
511 ipi_send(cpu, IPI_RESCHEDULE);
515 * arch_send_call_function_ipi()
516 * Cause each cpu in the mask to call the generic function handler.
518 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
521 for_each_cpu_mask(cpu, *mask) {
522 ipi_send(cpu, IPI_CALL_FUNC);
527 * arch_send_call_function_single_ipi()
528 * Cause the specified cpu to call the generic function handler.
530 void arch_send_call_function_single_ipi(int cpu)
532 ipi_send(cpu, IPI_CALL_FUNC_SINGLE);
536 * setup_profiling_timer()
537 * Dummy function created to keep Oprofile happy in the SMP case.
539 int setup_profiling_timer(unsigned int multiplier)
545 * smp_mainline_start()
546 * Start a slave thread executing a mainline Linux context.
548 static void __init smp_mainline_start(void *arg)
550 int cpuid = smp_processor_id();
551 struct cpuinfo_ubicom32 *p = &per_cpu(cpu_data, cpuid);
553 BUG_ON(p->tid != thread_get_self());
556 * Well, support 2.4 linux scheme as well.
558 if (cpu_test_and_set(cpuid, cpu_online_map)) {
559 printk(KERN_CRIT "cpu[%d]: already initialized!\n", cpuid);
560 smp_halt_processor();
565 * Initialise the idle task for this CPU
567 atomic_inc(&init_mm.mm_count);
568 current->active_mm = &init_mm;
570 printk(KERN_CRIT "cpu[%d]: idle task already has memory "
571 "management\n", cpuid);
572 smp_halt_processor();
577 * TODO: X86 does this prior to calling notify, try to understand why?
581 #if defined(CONFIG_GENERIC_CLOCKEVENTS)
583 * Setup a local timer event so that this cpu will get timer interrupts
585 if (local_timer_setup(cpuid) == -1) {
586 printk(KERN_CRIT "cpu[%d]: timer alloc failed\n", cpuid);
587 smp_halt_processor();
593 * Notify those interested that we are up and alive. This must
594 * be done before interrupts are enabled. It must also be completed
595 * before the bootstrap cpu returns from __cpu_up() (see comment
596 * above cpu_set() of the cpu_online_map).
598 notify_cpu_starting(cpuid);
601 * Indicate that this thread is now online and present. Setting
602 * cpu_online_map has the side effect of allowing the bootstrap
603 * cpu to continue along; so anything that MUST be done prior to the
604 * bootstrap cpu returning from __cpu_up() needs to go above here.
606 cpu_set(cpuid, cpu_online_map);
607 cpu_set(cpuid, cpu_present_map);
610 * Maintain a thread mapping in addition to the cpu mapping.
612 smp_online_threads |= (1 << p->tid);
615 * Enable interrupts for this thread.
620 * Enter the idle loop and wait for a timer to schedule some work.
622 printk(KERN_INFO "cpu[%d]: entering cpu_idle()\n", cpuid);
630 * Called once the kernel_init() has brought up all cpu(s).
632 void smp_cpus_done(unsigned int cpu_max)
639 * Called to startup a sepcific cpu.
641 int __cpuinit __cpu_up(unsigned int cpu)
643 struct task_struct *idle;
646 struct cpuinfo_ubicom32 *p = &per_cpu(cpu_data, cpu);
649 * Create an idle task for this CPU.
651 idle = fork_idle(cpu);
653 panic("cpu[%d]: fork failed\n", cpu);
656 task_thread_info(idle)->cpu = cpu;
659 * Setup the sw_ksp[] to point to this new task.
661 sw_ksp[p->tid] = (unsigned int)idle->stack;
662 stack = (unsigned int *)(sw_ksp[p->tid] + PAGE_SIZE - 8);
665 * Cause the specified thread to execute our smp_mainline_start
666 * function as a TYPE_NORMAL thread.
668 printk(KERN_INFO "cpu[%d]: launching mainline Linux thread\n", cpu);
669 if (thread_start(p->tid, smp_mainline_start, (void *)NULL, stack,
670 THREAD_TYPE_NORMAL) == -1) {
671 printk(KERN_WARNING "cpu[%d]: failed thread_start\n", cpu);
676 * Wait for the thread to start up. The thread will set
677 * the online bit when it is running. Our caller execpts the
678 * cpu to be online if we return 0.
680 for (timeout = 0; timeout < 10000; timeout++) {
681 if (cpu_online(cpu)) {
690 if (!cpu_online(cpu)) {
691 printk(KERN_CRIT "cpu[%d]: failed to live after %ld us\n",
696 printk(KERN_INFO "cpu[%d]: came alive after %ld us\n",
702 * Data used by setup_irq for the IPI.
704 static struct irqaction ipi_irq = {
706 .flags = IRQF_DISABLED | IRQF_PERCPU,
707 .handler = ipi_interrupt,
712 * Mark threads that are available to Linux as possible cpus(s).
714 void __init smp_prepare_cpus(unsigned int max_cpus)
719 * We will need a software IRQ to send IPI(s). We will use
720 * a single software IRQ for all IPI(s).
722 if (irq_soft_alloc(&smp_ipi_irq) < 0) {
723 panic("no software IRQ is available\n");
728 * For the IPI interrupt, we want to use our own chip definition.
729 * This allows us to define what happens in SMP IPI without affecting
730 * the performance of the other interrupts.
732 * Next, Register the IPI interrupt function against the soft IRQ.
734 set_irq_chip(smp_ipi_irq, &ubicom32_smp_chip);
735 setup_irq(smp_ipi_irq, &ipi_irq);
738 * We use the device tree node to determine how many
739 * free cpus we will have (up to NR_CPUS) and we indicate
740 * that those cpus are present.
742 * We need to do this very early in the SMP case
743 * because the Linux init code uses the cpu_present_map.
745 for_each_possible_cpu(i) {
747 struct cpuinfo_ubicom32 *p = &per_cpu(cpu_data, i);
750 * Skip the bootstrap cpu
757 * If we have a free thread left in the mask,
758 * indicate that the cpu is present.
760 tid = thread_alloc();
761 if (tid == (thread_t)-1) {
766 * Save the hardware thread id for this cpu.
769 cpu_set(i, cpu_present_map);
770 printk(KERN_INFO "cpu[%d]: added to cpu_present_map - tid: %d\n", i, tid);
775 * smp_prepare_boot_cpu()
776 * Copy the per_cpu data into the appropriate spot for the bootstrap cpu.
778 * The code in boot_cpu_init() has already set the boot cpu's
779 * state in the possible, present, and online maps.
781 void __devinit smp_prepare_boot_cpu(void)
783 struct cpuinfo_ubicom32 *p = &per_cpu(cpu_data, 0);
785 smp_online_threads |= (1 << p->tid);
786 printk(KERN_INFO "cpu[%d]: bootstrap CPU online - tid: %ld\n",
787 current_thread_info()->cpu, p->tid);
791 * smp_setup_processor_id()
792 * Set the current_thread_info() structure cpu value.
794 * We set the value to the true hardware thread value that we are running on.
795 * NOTE: this function overrides the weak alias function in main.c
797 void __init smp_setup_processor_id(void)
799 struct cpuinfo_ubicom32 *p = &per_cpu(cpu_data, 0);
801 for_each_cpu_mask(i, CPU_MASK_ALL)
802 set_cpu_possible(i, true);
804 current_thread_info()->cpu = 0;
805 p->tid = thread_get_self();