2 * linux/arch/m32r/kernel/smp.c
4 * M32R SMP support routines.
6 * Copyright (c) 2001, 2002 Hitoshi Yamamoto
8 * Taken from i386 version.
9 * (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
10 * (c) 1998-99, 2000 Ingo Molnar <mingo@redhat.com>
12 * This code is released under the GNU General Public License version 2 or
18 #include <linux/irq.h>
19 #include <linux/interrupt.h>
20 #include <linux/sched.h>
21 #include <linux/spinlock.h>
23 #include <linux/smp.h>
24 #include <linux/profile.h>
25 #include <linux/cpu.h>
27 #include <asm/cacheflush.h>
28 #include <asm/pgalloc.h>
29 #include <asm/atomic.h>
31 #include <asm/mmu_context.h>
34 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
35 /* Data structures and variables */
36 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
39 * For flush_cache_all()
41 static DEFINE_SPINLOCK(flushcache_lock);
42 static volatile unsigned long flushcache_cpumask = 0;
45 * For flush_tlb_others()
47 static volatile cpumask_t flush_cpumask;
48 static struct mm_struct *flush_mm;
49 static struct vm_area_struct *flush_vma;
50 static volatile unsigned long flush_va;
51 static DEFINE_SPINLOCK(tlbstate_lock);
52 #define FLUSH_ALL 0xffffffff
54 DECLARE_PER_CPU(int, prof_multiplier);
55 DECLARE_PER_CPU(int, prof_old_multiplier);
56 DECLARE_PER_CPU(int, prof_counter);
58 extern spinlock_t ipi_lock[];
60 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
61 /* Function Prototypes */
62 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
64 void smp_send_reschedule(int);
65 void smp_reschedule_interrupt(void);
67 void smp_flush_cache_all(void);
68 void smp_flush_cache_all_interrupt(void);
70 void smp_flush_tlb_all(void);
71 static void flush_tlb_all_ipi(void *);
73 void smp_flush_tlb_mm(struct mm_struct *);
74 void smp_flush_tlb_range(struct vm_area_struct *, unsigned long, \
76 void smp_flush_tlb_page(struct vm_area_struct *, unsigned long);
77 static void flush_tlb_others(cpumask_t, struct mm_struct *,
78 struct vm_area_struct *, unsigned long);
79 void smp_invalidate_interrupt(void);
81 void smp_send_stop(void);
82 static void stop_this_cpu(void *);
84 void smp_send_timer(void);
85 void smp_ipi_timer_interrupt(struct pt_regs *);
86 void smp_local_timer_interrupt(void);
88 static void send_IPI_allbutself(int, int);
89 static void send_IPI_mask(const struct cpumask *, int, int);
90 unsigned long send_IPI_mask_phys(cpumask_t, int, int);
92 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
93 /* Rescheduling request Routines */
94 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
96 /*==========================================================================*
97 * Name: smp_send_reschedule
99 * Description: This routine requests other CPU to execute rescheduling.
100 * 1.Send 'RESCHEDULE_IPI' to other CPU.
101 * Request other CPU to execute 'smp_reschedule_interrupt()'.
103 * Born on Date: 2002.02.05
105 * Arguments: cpu_id - Target CPU ID
107 * Returns: void (cannot fail)
110 * Date Who Description
111 * ---------- --- --------------------------------------------------------
113 *==========================================================================*/
114 void smp_send_reschedule(int cpu_id)
116 WARN_ON(cpu_is_offline(cpu_id));
117 send_IPI_mask(cpumask_of(cpu_id), RESCHEDULE_IPI, 1);
120 /*==========================================================================*
121 * Name: smp_reschedule_interrupt
123 * Description: This routine executes on CPU which received
126 * Born on Date: 2002.02.05
130 * Returns: void (cannot fail)
133 * Date Who Description
134 * ---------- --- --------------------------------------------------------
136 *==========================================================================*/
137 void smp_reschedule_interrupt(void)
142 /*==========================================================================*
143 * Name: smp_flush_cache_all
145 * Description: This routine sends a 'INVALIDATE_CACHE_IPI' to all other
146 * CPUs in the system.
148 * Born on Date: 2003-05-28
152 * Returns: void (cannot fail)
155 * Date Who Description
156 * ---------- --- --------------------------------------------------------
158 *==========================================================================*/
159 void smp_flush_cache_all(void)
165 cpumask = cpu_online_map;
166 cpu_clear(smp_processor_id(), cpumask);
167 spin_lock(&flushcache_lock);
168 mask=cpus_addr(cpumask);
169 atomic_set_mask(*mask, (atomic_t *)&flushcache_cpumask);
170 send_IPI_mask(&cpumask, INVALIDATE_CACHE_IPI, 0);
171 _flush_cache_copyback_all();
172 while (flushcache_cpumask)
174 spin_unlock(&flushcache_lock);
178 void smp_flush_cache_all_interrupt(void)
180 _flush_cache_copyback_all();
181 clear_bit(smp_processor_id(), &flushcache_cpumask);
184 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
185 /* TLB flush request Routines */
186 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
188 /*==========================================================================*
189 * Name: smp_flush_tlb_all
191 * Description: This routine flushes all processes TLBs.
192 * 1.Request other CPU to execute 'flush_tlb_all_ipi()'.
193 * 2.Execute 'do_flush_tlb_all_local()'.
195 * Born on Date: 2002.02.05
199 * Returns: void (cannot fail)
202 * Date Who Description
203 * ---------- --- --------------------------------------------------------
205 *==========================================================================*/
206 void smp_flush_tlb_all(void)
211 local_irq_save(flags);
213 local_irq_restore(flags);
214 smp_call_function(flush_tlb_all_ipi, NULL, 1);
218 /*==========================================================================*
219 * Name: flush_tlb_all_ipi
221 * Description: This routine flushes all local TLBs.
222 * 1.Execute 'do_flush_tlb_all_local()'.
224 * Born on Date: 2002.02.05
226 * Arguments: *info - not used
228 * Returns: void (cannot fail)
231 * Date Who Description
232 * ---------- --- --------------------------------------------------------
234 *==========================================================================*/
235 static void flush_tlb_all_ipi(void *info)
240 /*==========================================================================*
241 * Name: smp_flush_tlb_mm
243 * Description: This routine flushes the specified mm context TLB's.
245 * Born on Date: 2002.02.05
247 * Arguments: *mm - a pointer to the mm struct for flush TLB
249 * Returns: void (cannot fail)
252 * Date Who Description
253 * ---------- --- --------------------------------------------------------
255 *==========================================================================*/
256 void smp_flush_tlb_mm(struct mm_struct *mm)
264 cpu_id = smp_processor_id();
265 mmc = &mm->context[cpu_id];
266 cpu_mask = *mm_cpumask(mm);
267 cpu_clear(cpu_id, cpu_mask);
269 if (*mmc != NO_CONTEXT) {
270 local_irq_save(flags);
272 if (mm == current->mm)
273 activate_context(mm);
275 cpumask_clear_cpu(cpu_id, mm_cpumask(mm));
276 local_irq_restore(flags);
278 if (!cpus_empty(cpu_mask))
279 flush_tlb_others(cpu_mask, mm, NULL, FLUSH_ALL);
284 /*==========================================================================*
285 * Name: smp_flush_tlb_range
287 * Description: This routine flushes a range of pages.
289 * Born on Date: 2002.02.05
291 * Arguments: *mm - a pointer to the mm struct for flush TLB
295 * Returns: void (cannot fail)
298 * Date Who Description
299 * ---------- --- --------------------------------------------------------
301 *==========================================================================*/
302 void smp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
305 smp_flush_tlb_mm(vma->vm_mm);
308 /*==========================================================================*
309 * Name: smp_flush_tlb_page
311 * Description: This routine flushes one page.
313 * Born on Date: 2002.02.05
315 * Arguments: *vma - a pointer to the vma struct include va
316 * va - virtual address for flush TLB
318 * Returns: void (cannot fail)
321 * Date Who Description
322 * ---------- --- --------------------------------------------------------
324 *==========================================================================*/
325 void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long va)
327 struct mm_struct *mm = vma->vm_mm;
334 cpu_id = smp_processor_id();
335 mmc = &mm->context[cpu_id];
336 cpu_mask = *mm_cpumask(mm);
337 cpu_clear(cpu_id, cpu_mask);
344 if (*mmc != NO_CONTEXT) {
345 local_irq_save(flags);
347 va |= (*mmc & MMU_CONTEXT_ASID_MASK);
348 __flush_tlb_page(va);
349 local_irq_restore(flags);
351 if (!cpus_empty(cpu_mask))
352 flush_tlb_others(cpu_mask, mm, vma, va);
357 /*==========================================================================*
358 * Name: flush_tlb_others
360 * Description: This routine requests other CPU to execute flush TLB.
361 * 1.Setup parameters.
362 * 2.Send 'INVALIDATE_TLB_IPI' to other CPU.
363 * Request other CPU to execute 'smp_invalidate_interrupt()'.
364 * 3.Wait for other CPUs operation finished.
366 * Born on Date: 2002.02.05
368 * Arguments: cpumask - bitmap of target CPUs
369 * *mm - a pointer to the mm struct for flush TLB
370 * *vma - a pointer to the vma struct include va
371 * va - virtual address for flush TLB
373 * Returns: void (cannot fail)
376 * Date Who Description
377 * ---------- --- --------------------------------------------------------
379 *==========================================================================*/
380 static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
381 struct vm_area_struct *vma, unsigned long va)
387 if (!(flags & 0x0040)) /* Interrupt Disable NONONO */
389 #endif /* DEBUG_SMP */
392 * A couple of (to be removed) sanity checks:
394 * - we do not send IPIs to not-yet booted CPUs.
395 * - current CPU must not be in mask
396 * - mask must exist :)
398 BUG_ON(cpus_empty(cpumask));
400 BUG_ON(cpu_isset(smp_processor_id(), cpumask));
403 /* If a CPU which we ran on has gone down, OK. */
404 cpus_and(cpumask, cpumask, cpu_online_map);
405 if (cpus_empty(cpumask))
409 * i'm not happy about this global shared spinlock in the
410 * MM hot path, but we'll see how contended it is.
411 * Temporarily this turns IRQs off, so that lockups are
412 * detected by the NMI watchdog.
414 spin_lock(&tlbstate_lock);
419 mask=cpus_addr(cpumask);
420 atomic_set_mask(*mask, (atomic_t *)&flush_cpumask);
423 * We have to send the IPI only to
426 send_IPI_mask(&cpumask, INVALIDATE_TLB_IPI, 0);
428 while (!cpus_empty(flush_cpumask)) {
429 /* nothing. lockup detection does not belong here */
436 spin_unlock(&tlbstate_lock);
439 /*==========================================================================*
440 * Name: smp_invalidate_interrupt
442 * Description: This routine executes on CPU which received
443 * 'INVALIDATE_TLB_IPI'.
445 * 2.Report flush TLB process was finished.
447 * Born on Date: 2002.02.05
451 * Returns: void (cannot fail)
454 * Date Who Description
455 * ---------- --- --------------------------------------------------------
457 *==========================================================================*/
458 void smp_invalidate_interrupt(void)
460 int cpu_id = smp_processor_id();
461 unsigned long *mmc = &flush_mm->context[cpu_id];
463 if (!cpu_isset(cpu_id, flush_cpumask))
466 if (flush_va == FLUSH_ALL) {
468 if (flush_mm == current->active_mm)
469 activate_context(flush_mm);
471 cpumask_clear_cpu(cpu_id, mm_cpumask(flush_mm));
473 unsigned long va = flush_va;
475 if (*mmc != NO_CONTEXT) {
477 va |= (*mmc & MMU_CONTEXT_ASID_MASK);
478 __flush_tlb_page(va);
481 cpu_clear(cpu_id, flush_cpumask);
484 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
485 /* Stop CPU request Routines */
486 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
488 /*==========================================================================*
489 * Name: smp_send_stop
491 * Description: This routine requests stop all CPUs.
492 * 1.Request other CPU to execute 'stop_this_cpu()'.
494 * Born on Date: 2002.02.05
498 * Returns: void (cannot fail)
501 * Date Who Description
502 * ---------- --- --------------------------------------------------------
504 *==========================================================================*/
505 void smp_send_stop(void)
507 smp_call_function(stop_this_cpu, NULL, 0);
510 /*==========================================================================*
511 * Name: stop_this_cpu
513 * Description: This routine halt CPU.
515 * Born on Date: 2002.02.05
519 * Returns: void (cannot fail)
522 * Date Who Description
523 * ---------- --- --------------------------------------------------------
525 *==========================================================================*/
526 static void stop_this_cpu(void *dummy)
528 int cpu_id = smp_processor_id();
533 cpu_clear(cpu_id, cpu_online_map);
541 outl(0, M32R_ICU_IMASK_PORTL);
542 inl(M32R_ICU_IMASK_PORTL); /* dummy read */
548 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
550 send_IPI_mask(mask, CALL_FUNCTION_IPI, 0);
553 void arch_send_call_function_single_ipi(int cpu)
555 send_IPI_mask(cpumask_of(cpu), CALL_FUNC_SINGLE_IPI, 0);
558 /*==========================================================================*
559 * Name: smp_call_function_interrupt
561 * Description: This routine executes on CPU which received
562 * 'CALL_FUNCTION_IPI'.
564 * Born on Date: 2002.02.05
568 * Returns: void (cannot fail)
571 * Date Who Description
572 * ---------- --- --------------------------------------------------------
574 *==========================================================================*/
575 void smp_call_function_interrupt(void)
578 generic_smp_call_function_interrupt();
582 void smp_call_function_single_interrupt(void)
585 generic_smp_call_function_single_interrupt();
589 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
591 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
593 /*==========================================================================*
594 * Name: smp_send_timer
596 * Description: This routine sends a 'LOCAL_TIMER_IPI' to all other CPUs
599 * Born on Date: 2002.02.05
603 * Returns: void (cannot fail)
606 * Date Who Description
607 * ---------- --- --------------------------------------------------------
609 *==========================================================================*/
610 void smp_send_timer(void)
612 send_IPI_allbutself(LOCAL_TIMER_IPI, 1);
615 /*==========================================================================*
616 * Name: smp_send_timer
618 * Description: This routine executes on CPU which received
621 * Born on Date: 2002.02.05
623 * Arguments: *regs - a pointer to the saved regster info
625 * Returns: void (cannot fail)
628 * Date Who Description
629 * ---------- --- --------------------------------------------------------
631 *==========================================================================*/
632 void smp_ipi_timer_interrupt(struct pt_regs *regs)
634 struct pt_regs *old_regs;
635 old_regs = set_irq_regs(regs);
637 smp_local_timer_interrupt();
639 set_irq_regs(old_regs);
642 /*==========================================================================*
643 * Name: smp_local_timer_interrupt
645 * Description: Local timer interrupt handler. It does both profiling and
646 * process statistics/rescheduling.
647 * We do profiling in every local tick, statistics/rescheduling
648 * happen only every 'profiling multiplier' ticks. The default
649 * multiplier is 1 and it can be changed by writing the new
650 * multiplier value into /proc/profile.
652 * Born on Date: 2002.02.05
654 * Arguments: *regs - a pointer to the saved regster info
656 * Returns: void (cannot fail)
658 * Original: arch/i386/kernel/apic.c
661 * Date Who Description
662 * ---------- --- --------------------------------------------------------
663 * 2003-06-24 hy use per_cpu structure.
664 *==========================================================================*/
665 void smp_local_timer_interrupt(void)
667 int user = user_mode(get_irq_regs());
668 int cpu_id = smp_processor_id();
671 * The profiling function is SMP safe. (nothing can mess
672 * around with "current", and the profiling counters are
673 * updated with atomic operations). This is especially
674 * useful with a profiling multiplier != 1
677 profile_tick(CPU_PROFILING);
679 if (--per_cpu(prof_counter, cpu_id) <= 0) {
681 * The multiplier may have changed since the last time we got
682 * to this point as a result of the user writing to
683 * /proc/profile. In this case we need to adjust the APIC
686 * Interrupts are already masked off at this point.
688 per_cpu(prof_counter, cpu_id)
689 = per_cpu(prof_multiplier, cpu_id);
690 if (per_cpu(prof_counter, cpu_id)
691 != per_cpu(prof_old_multiplier, cpu_id))
693 per_cpu(prof_old_multiplier, cpu_id)
694 = per_cpu(prof_counter, cpu_id);
697 update_process_times(user);
701 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
702 /* Send IPI Routines */
703 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
705 /*==========================================================================*
706 * Name: send_IPI_allbutself
708 * Description: This routine sends a IPI to all other CPUs in the system.
710 * Born on Date: 2002.02.05
712 * Arguments: ipi_num - Number of IPI
713 * try - 0 : Send IPI certainly.
714 * !0 : The following IPI is not sent when Target CPU
715 * has not received the before IPI.
717 * Returns: void (cannot fail)
720 * Date Who Description
721 * ---------- --- --------------------------------------------------------
723 *==========================================================================*/
724 static void send_IPI_allbutself(int ipi_num, int try)
728 cpumask = cpu_online_map;
729 cpu_clear(smp_processor_id(), cpumask);
731 send_IPI_mask(&cpumask, ipi_num, try);
734 /*==========================================================================*
735 * Name: send_IPI_mask
737 * Description: This routine sends a IPI to CPUs in the system.
739 * Born on Date: 2002.02.05
741 * Arguments: cpu_mask - Bitmap of target CPUs logical ID
742 * ipi_num - Number of IPI
743 * try - 0 : Send IPI certainly.
744 * !0 : The following IPI is not sent when Target CPU
745 * has not received the before IPI.
747 * Returns: void (cannot fail)
750 * Date Who Description
751 * ---------- --- --------------------------------------------------------
753 *==========================================================================*/
754 static void send_IPI_mask(const struct cpumask *cpumask, int ipi_num, int try)
756 cpumask_t physid_mask, tmp;
758 int num_cpus = num_online_cpus();
760 if (num_cpus <= 1) /* NO MP */
763 cpumask_and(&tmp, cpumask, cpu_online_mask);
764 BUG_ON(!cpumask_equal(cpumask, &tmp));
766 physid_mask = CPU_MASK_NONE;
767 for_each_cpu(cpu_id, cpumask) {
768 if ((phys_id = cpu_to_physid(cpu_id)) != -1)
769 cpu_set(phys_id, physid_mask);
772 send_IPI_mask_phys(physid_mask, ipi_num, try);
775 /*==========================================================================*
776 * Name: send_IPI_mask_phys
778 * Description: This routine sends a IPI to other CPUs in the system.
780 * Born on Date: 2002.02.05
782 * Arguments: cpu_mask - Bitmap of target CPUs physical ID
783 * ipi_num - Number of IPI
784 * try - 0 : Send IPI certainly.
785 * !0 : The following IPI is not sent when Target CPU
786 * has not received the before IPI.
788 * Returns: IPICRi regster value.
791 * Date Who Description
792 * ---------- --- --------------------------------------------------------
794 *==========================================================================*/
795 unsigned long send_IPI_mask_phys(cpumask_t physid_mask, int ipi_num,
799 volatile unsigned long *ipicr_addr;
800 unsigned long ipicr_val;
801 unsigned long my_physid_mask;
802 unsigned long mask = cpus_addr(physid_mask)[0];
805 if (mask & ~physids_coerce(phys_cpu_present_map))
807 if (ipi_num >= NR_IPIS || ipi_num < 0)
811 ipilock = &ipi_lock[ipi_num];
812 ipicr_addr = (volatile unsigned long *)(M32R_ICU_IPICR_ADDR
814 my_physid_mask = ~(1 << smp_processor_id());
819 * write IPICRi (send IPIi)
823 __asm__ __volatile__ (
824 ";; CHECK IPICRi == 0 \n\t"
832 ";; WRITE IPICRi (send IPIi) \n\t"
839 : "r"(ipicr_addr), "r"(mask), "r"(try), "r"(my_physid_mask)
842 spin_unlock(ipilock);