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/spinlock.h>
22 #include <linux/smp.h>
23 #include <linux/profile.h>
24 #include <linux/cpu.h>
26 #include <asm/cacheflush.h>
27 #include <asm/pgalloc.h>
28 #include <asm/atomic.h>
30 #include <asm/mmu_context.h>
33 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
34 /* Data structures and variables */
35 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
38 * For flush_cache_all()
40 static DEFINE_SPINLOCK(flushcache_lock);
41 static volatile unsigned long flushcache_cpumask = 0;
44 * For flush_tlb_others()
46 static volatile cpumask_t flush_cpumask;
47 static struct mm_struct *flush_mm;
48 static struct vm_area_struct *flush_vma;
49 static volatile unsigned long flush_va;
50 static DEFINE_SPINLOCK(tlbstate_lock);
51 #define FLUSH_ALL 0xffffffff
53 DECLARE_PER_CPU(int, prof_multiplier);
54 DECLARE_PER_CPU(int, prof_old_multiplier);
55 DECLARE_PER_CPU(int, prof_counter);
57 extern spinlock_t ipi_lock[];
59 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
60 /* Function Prototypes */
61 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
63 void smp_send_reschedule(int);
64 void smp_reschedule_interrupt(void);
66 void smp_flush_cache_all(void);
67 void smp_flush_cache_all_interrupt(void);
69 void smp_flush_tlb_all(void);
70 static void flush_tlb_all_ipi(void *);
72 void smp_flush_tlb_mm(struct mm_struct *);
73 void smp_flush_tlb_range(struct vm_area_struct *, unsigned long, \
75 void smp_flush_tlb_page(struct vm_area_struct *, unsigned long);
76 static void flush_tlb_others(cpumask_t, struct mm_struct *,
77 struct vm_area_struct *, unsigned long);
78 void smp_invalidate_interrupt(void);
80 void smp_send_stop(void);
81 static void stop_this_cpu(void *);
83 void smp_send_timer(void);
84 void smp_ipi_timer_interrupt(struct pt_regs *);
85 void smp_local_timer_interrupt(void);
87 static void send_IPI_allbutself(int, int);
88 static void send_IPI_mask(cpumask_t, int, int);
89 unsigned long send_IPI_mask_phys(cpumask_t, int, int);
91 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
92 /* Rescheduling request Routines */
93 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
95 /*==========================================================================*
96 * Name: smp_send_reschedule
98 * Description: This routine requests other CPU to execute rescheduling.
99 * 1.Send 'RESCHEDULE_IPI' to other CPU.
100 * Request other CPU to execute 'smp_reschedule_interrupt()'.
102 * Born on Date: 2002.02.05
104 * Arguments: cpu_id - Target CPU ID
106 * Returns: void (cannot fail)
109 * Date Who Description
110 * ---------- --- --------------------------------------------------------
112 *==========================================================================*/
113 void smp_send_reschedule(int cpu_id)
115 WARN_ON(cpu_is_offline(cpu_id));
116 send_IPI_mask(cpumask_of_cpu(cpu_id), RESCHEDULE_IPI, 1);
119 /*==========================================================================*
120 * Name: smp_reschedule_interrupt
122 * Description: This routine executes on CPU which received
124 * Rescheduling is processed at the exit of interrupt
127 * Born on Date: 2002.02.05
131 * Returns: void (cannot fail)
134 * Date Who Description
135 * ---------- --- --------------------------------------------------------
137 *==========================================================================*/
138 void smp_reschedule_interrupt(void)
143 /*==========================================================================*
144 * Name: smp_flush_cache_all
146 * Description: This routine sends a 'INVALIDATE_CACHE_IPI' to all other
147 * CPUs in the system.
149 * Born on Date: 2003-05-28
153 * Returns: void (cannot fail)
156 * Date Who Description
157 * ---------- --- --------------------------------------------------------
159 *==========================================================================*/
160 void smp_flush_cache_all(void)
166 cpumask = cpu_online_map;
167 cpu_clear(smp_processor_id(), cpumask);
168 spin_lock(&flushcache_lock);
169 mask=cpus_addr(cpumask);
170 atomic_set_mask(*mask, (atomic_t *)&flushcache_cpumask);
171 send_IPI_mask(cpumask, INVALIDATE_CACHE_IPI, 0);
172 _flush_cache_copyback_all();
173 while (flushcache_cpumask)
175 spin_unlock(&flushcache_lock);
179 void smp_flush_cache_all_interrupt(void)
181 _flush_cache_copyback_all();
182 clear_bit(smp_processor_id(), &flushcache_cpumask);
185 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
186 /* TLB flush request Routines */
187 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
189 /*==========================================================================*
190 * Name: smp_flush_tlb_all
192 * Description: This routine flushes all processes TLBs.
193 * 1.Request other CPU to execute 'flush_tlb_all_ipi()'.
194 * 2.Execute 'do_flush_tlb_all_local()'.
196 * Born on Date: 2002.02.05
200 * Returns: void (cannot fail)
203 * Date Who Description
204 * ---------- --- --------------------------------------------------------
206 *==========================================================================*/
207 void smp_flush_tlb_all(void)
212 local_irq_save(flags);
214 local_irq_restore(flags);
215 smp_call_function(flush_tlb_all_ipi, NULL, 1);
219 /*==========================================================================*
220 * Name: flush_tlb_all_ipi
222 * Description: This routine flushes all local TLBs.
223 * 1.Execute 'do_flush_tlb_all_local()'.
225 * Born on Date: 2002.02.05
227 * Arguments: *info - not used
229 * Returns: void (cannot fail)
232 * Date Who Description
233 * ---------- --- --------------------------------------------------------
235 *==========================================================================*/
236 static void flush_tlb_all_ipi(void *info)
241 /*==========================================================================*
242 * Name: smp_flush_tlb_mm
244 * Description: This routine flushes the specified mm context TLB's.
246 * Born on Date: 2002.02.05
248 * Arguments: *mm - a pointer to the mm struct for flush TLB
250 * Returns: void (cannot fail)
253 * Date Who Description
254 * ---------- --- --------------------------------------------------------
256 *==========================================================================*/
257 void smp_flush_tlb_mm(struct mm_struct *mm)
265 cpu_id = smp_processor_id();
266 mmc = &mm->context[cpu_id];
267 cpu_mask = mm->cpu_vm_mask;
268 cpu_clear(cpu_id, cpu_mask);
270 if (*mmc != NO_CONTEXT) {
271 local_irq_save(flags);
273 if (mm == current->mm)
274 activate_context(mm);
276 cpu_clear(cpu_id, mm->cpu_vm_mask);
277 local_irq_restore(flags);
279 if (!cpus_empty(cpu_mask))
280 flush_tlb_others(cpu_mask, mm, NULL, FLUSH_ALL);
285 /*==========================================================================*
286 * Name: smp_flush_tlb_range
288 * Description: This routine flushes a range of pages.
290 * Born on Date: 2002.02.05
292 * Arguments: *mm - a pointer to the mm struct for flush TLB
296 * Returns: void (cannot fail)
299 * Date Who Description
300 * ---------- --- --------------------------------------------------------
302 *==========================================================================*/
303 void smp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
306 smp_flush_tlb_mm(vma->vm_mm);
309 /*==========================================================================*
310 * Name: smp_flush_tlb_page
312 * Description: This routine flushes one page.
314 * Born on Date: 2002.02.05
316 * Arguments: *vma - a pointer to the vma struct include va
317 * va - virtual address for flush TLB
319 * Returns: void (cannot fail)
322 * Date Who Description
323 * ---------- --- --------------------------------------------------------
325 *==========================================================================*/
326 void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long va)
328 struct mm_struct *mm = vma->vm_mm;
335 cpu_id = smp_processor_id();
336 mmc = &mm->context[cpu_id];
337 cpu_mask = mm->cpu_vm_mask;
338 cpu_clear(cpu_id, cpu_mask);
345 if (*mmc != NO_CONTEXT) {
346 local_irq_save(flags);
348 va |= (*mmc & MMU_CONTEXT_ASID_MASK);
349 __flush_tlb_page(va);
350 local_irq_restore(flags);
352 if (!cpus_empty(cpu_mask))
353 flush_tlb_others(cpu_mask, mm, vma, va);
358 /*==========================================================================*
359 * Name: flush_tlb_others
361 * Description: This routine requests other CPU to execute flush TLB.
362 * 1.Setup parameters.
363 * 2.Send 'INVALIDATE_TLB_IPI' to other CPU.
364 * Request other CPU to execute 'smp_invalidate_interrupt()'.
365 * 3.Wait for other CPUs operation finished.
367 * Born on Date: 2002.02.05
369 * Arguments: cpumask - bitmap of target CPUs
370 * *mm - a pointer to the mm struct for flush TLB
371 * *vma - a pointer to the vma struct include va
372 * va - virtual address for flush TLB
374 * Returns: void (cannot fail)
377 * Date Who Description
378 * ---------- --- --------------------------------------------------------
380 *==========================================================================*/
381 static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
382 struct vm_area_struct *vma, unsigned long va)
388 if (!(flags & 0x0040)) /* Interrupt Disable NONONO */
390 #endif /* DEBUG_SMP */
393 * A couple of (to be removed) sanity checks:
395 * - we do not send IPIs to not-yet booted CPUs.
396 * - current CPU must not be in mask
397 * - mask must exist :)
399 BUG_ON(cpus_empty(cpumask));
401 BUG_ON(cpu_isset(smp_processor_id(), cpumask));
404 /* If a CPU which we ran on has gone down, OK. */
405 cpus_and(cpumask, cpumask, cpu_online_map);
406 if (cpus_empty(cpumask))
410 * i'm not happy about this global shared spinlock in the
411 * MM hot path, but we'll see how contended it is.
412 * Temporarily this turns IRQs off, so that lockups are
413 * detected by the NMI watchdog.
415 spin_lock(&tlbstate_lock);
420 mask=cpus_addr(cpumask);
421 atomic_set_mask(*mask, (atomic_t *)&flush_cpumask);
424 * We have to send the IPI only to
427 send_IPI_mask(cpumask, INVALIDATE_TLB_IPI, 0);
429 while (!cpus_empty(flush_cpumask)) {
430 /* nothing. lockup detection does not belong here */
437 spin_unlock(&tlbstate_lock);
440 /*==========================================================================*
441 * Name: smp_invalidate_interrupt
443 * Description: This routine executes on CPU which received
444 * 'INVALIDATE_TLB_IPI'.
446 * 2.Report flush TLB process was finished.
448 * Born on Date: 2002.02.05
452 * Returns: void (cannot fail)
455 * Date Who Description
456 * ---------- --- --------------------------------------------------------
458 *==========================================================================*/
459 void smp_invalidate_interrupt(void)
461 int cpu_id = smp_processor_id();
462 unsigned long *mmc = &flush_mm->context[cpu_id];
464 if (!cpu_isset(cpu_id, flush_cpumask))
467 if (flush_va == FLUSH_ALL) {
469 if (flush_mm == current->active_mm)
470 activate_context(flush_mm);
472 cpu_clear(cpu_id, flush_mm->cpu_vm_mask);
474 unsigned long va = flush_va;
476 if (*mmc != NO_CONTEXT) {
478 va |= (*mmc & MMU_CONTEXT_ASID_MASK);
479 __flush_tlb_page(va);
482 cpu_clear(cpu_id, flush_cpumask);
485 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
486 /* Stop CPU request Routines */
487 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
489 /*==========================================================================*
490 * Name: smp_send_stop
492 * Description: This routine requests stop all CPUs.
493 * 1.Request other CPU to execute 'stop_this_cpu()'.
495 * Born on Date: 2002.02.05
499 * Returns: void (cannot fail)
502 * Date Who Description
503 * ---------- --- --------------------------------------------------------
505 *==========================================================================*/
506 void smp_send_stop(void)
508 smp_call_function(stop_this_cpu, NULL, 0);
511 /*==========================================================================*
512 * Name: stop_this_cpu
514 * Description: This routine halt CPU.
516 * Born on Date: 2002.02.05
520 * Returns: void (cannot fail)
523 * Date Who Description
524 * ---------- --- --------------------------------------------------------
526 *==========================================================================*/
527 static void stop_this_cpu(void *dummy)
529 int cpu_id = smp_processor_id();
534 cpu_clear(cpu_id, cpu_online_map);
542 outl(0, M32R_ICU_IMASK_PORTL);
543 inl(M32R_ICU_IMASK_PORTL); /* dummy read */
549 void arch_send_call_function_ipi(cpumask_t mask)
551 send_IPI_mask(mask, CALL_FUNCTION_IPI, 0);
554 void arch_send_call_function_single_ipi(int cpu)
556 send_IPI_mask(cpumask_of_cpu(cpu), CALL_FUNC_SINGLE_IPI, 0);
559 /*==========================================================================*
560 * Name: smp_call_function_interrupt
562 * Description: This routine executes on CPU which received
563 * 'CALL_FUNCTION_IPI'.
565 * Born on Date: 2002.02.05
569 * Returns: void (cannot fail)
572 * Date Who Description
573 * ---------- --- --------------------------------------------------------
575 *==========================================================================*/
576 void smp_call_function_interrupt(void)
579 generic_smp_call_function_interrupt();
583 void smp_call_function_single_interrupt(void)
586 generic_smp_call_function_single_interrupt();
590 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
592 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
594 /*==========================================================================*
595 * Name: smp_send_timer
597 * Description: This routine sends a 'LOCAL_TIMER_IPI' to all other CPUs
600 * Born on Date: 2002.02.05
604 * Returns: void (cannot fail)
607 * Date Who Description
608 * ---------- --- --------------------------------------------------------
610 *==========================================================================*/
611 void smp_send_timer(void)
613 send_IPI_allbutself(LOCAL_TIMER_IPI, 1);
616 /*==========================================================================*
617 * Name: smp_send_timer
619 * Description: This routine executes on CPU which received
622 * Born on Date: 2002.02.05
624 * Arguments: *regs - a pointer to the saved regster info
626 * Returns: void (cannot fail)
629 * Date Who Description
630 * ---------- --- --------------------------------------------------------
632 *==========================================================================*/
633 void smp_ipi_timer_interrupt(struct pt_regs *regs)
635 struct pt_regs *old_regs;
636 old_regs = set_irq_regs(regs);
638 smp_local_timer_interrupt();
640 set_irq_regs(old_regs);
643 /*==========================================================================*
644 * Name: smp_local_timer_interrupt
646 * Description: Local timer interrupt handler. It does both profiling and
647 * process statistics/rescheduling.
648 * We do profiling in every local tick, statistics/rescheduling
649 * happen only every 'profiling multiplier' ticks. The default
650 * multiplier is 1 and it can be changed by writing the new
651 * multiplier value into /proc/profile.
653 * Born on Date: 2002.02.05
655 * Arguments: *regs - a pointer to the saved regster info
657 * Returns: void (cannot fail)
659 * Original: arch/i386/kernel/apic.c
662 * Date Who Description
663 * ---------- --- --------------------------------------------------------
664 * 2003-06-24 hy use per_cpu structure.
665 *==========================================================================*/
666 void smp_local_timer_interrupt(void)
668 int user = user_mode(get_irq_regs());
669 int cpu_id = smp_processor_id();
672 * The profiling function is SMP safe. (nothing can mess
673 * around with "current", and the profiling counters are
674 * updated with atomic operations). This is especially
675 * useful with a profiling multiplier != 1
678 profile_tick(CPU_PROFILING);
680 if (--per_cpu(prof_counter, cpu_id) <= 0) {
682 * The multiplier may have changed since the last time we got
683 * to this point as a result of the user writing to
684 * /proc/profile. In this case we need to adjust the APIC
687 * Interrupts are already masked off at this point.
689 per_cpu(prof_counter, cpu_id)
690 = per_cpu(prof_multiplier, cpu_id);
691 if (per_cpu(prof_counter, cpu_id)
692 != per_cpu(prof_old_multiplier, cpu_id))
694 per_cpu(prof_old_multiplier, cpu_id)
695 = per_cpu(prof_counter, cpu_id);
698 update_process_times(user);
702 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
703 /* Send IPI Routines */
704 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
706 /*==========================================================================*
707 * Name: send_IPI_allbutself
709 * Description: This routine sends a IPI to all other CPUs in the system.
711 * Born on Date: 2002.02.05
713 * Arguments: ipi_num - Number of IPI
714 * try - 0 : Send IPI certainly.
715 * !0 : The following IPI is not sent when Target CPU
716 * has not received the before IPI.
718 * Returns: void (cannot fail)
721 * Date Who Description
722 * ---------- --- --------------------------------------------------------
724 *==========================================================================*/
725 static void send_IPI_allbutself(int ipi_num, int try)
729 cpumask = cpu_online_map;
730 cpu_clear(smp_processor_id(), cpumask);
732 send_IPI_mask(cpumask, ipi_num, try);
735 /*==========================================================================*
736 * Name: send_IPI_mask
738 * Description: This routine sends a IPI to CPUs in the system.
740 * Born on Date: 2002.02.05
742 * Arguments: cpu_mask - Bitmap of target CPUs logical ID
743 * ipi_num - Number of IPI
744 * try - 0 : Send IPI certainly.
745 * !0 : The following IPI is not sent when Target CPU
746 * has not received the before IPI.
748 * Returns: void (cannot fail)
751 * Date Who Description
752 * ---------- --- --------------------------------------------------------
754 *==========================================================================*/
755 static void send_IPI_mask(cpumask_t cpumask, int ipi_num, int try)
757 cpumask_t physid_mask, tmp;
759 int num_cpus = num_online_cpus();
761 if (num_cpus <= 1) /* NO MP */
764 cpus_and(tmp, cpumask, cpu_online_map);
765 BUG_ON(!cpus_equal(cpumask, tmp));
767 physid_mask = CPU_MASK_NONE;
768 for_each_cpu_mask(cpu_id, cpumask){
769 if ((phys_id = cpu_to_physid(cpu_id)) != -1)
770 cpu_set(phys_id, physid_mask);
773 send_IPI_mask_phys(physid_mask, ipi_num, try);
776 /*==========================================================================*
777 * Name: send_IPI_mask_phys
779 * Description: This routine sends a IPI to other CPUs in the system.
781 * Born on Date: 2002.02.05
783 * Arguments: cpu_mask - Bitmap of target CPUs physical ID
784 * ipi_num - Number of IPI
785 * try - 0 : Send IPI certainly.
786 * !0 : The following IPI is not sent when Target CPU
787 * has not received the before IPI.
789 * Returns: IPICRi regster value.
792 * Date Who Description
793 * ---------- --- --------------------------------------------------------
795 *==========================================================================*/
796 unsigned long send_IPI_mask_phys(cpumask_t physid_mask, int ipi_num,
800 volatile unsigned long *ipicr_addr;
801 unsigned long ipicr_val;
802 unsigned long my_physid_mask;
803 unsigned long mask = cpus_addr(physid_mask)[0];
806 if (mask & ~physids_coerce(phys_cpu_present_map))
808 if (ipi_num >= NR_IPIS)
812 ipilock = &ipi_lock[ipi_num];
813 ipicr_addr = (volatile unsigned long *)(M32R_ICU_IPICR_ADDR
815 my_physid_mask = ~(1 << smp_processor_id());
820 * write IPICRi (send IPIi)
824 __asm__ __volatile__ (
825 ";; CHECK IPICRi == 0 \n\t"
833 ";; WRITE IPICRi (send IPIi) \n\t"
840 : "r"(ipicr_addr), "r"(mask), "r"(try), "r"(my_physid_mask)
843 spin_unlock(ipilock);