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 <linux/atomic.h>
31 #include <asm/mmu_context.h>
33 #include <asm/tlbflush.h>
35 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
36 /* Data structures and variables */
37 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
40 * For flush_cache_all()
42 static DEFINE_SPINLOCK(flushcache_lock
);
43 static volatile unsigned long flushcache_cpumask
= 0;
46 * For flush_tlb_others()
48 static cpumask_t flush_cpumask
;
49 static struct mm_struct
*flush_mm
;
50 static struct vm_area_struct
*flush_vma
;
51 static volatile unsigned long flush_va
;
52 static DEFINE_SPINLOCK(tlbstate_lock
);
53 #define FLUSH_ALL 0xffffffff
55 DECLARE_PER_CPU(int, prof_multiplier
);
56 DECLARE_PER_CPU(int, prof_old_multiplier
);
57 DECLARE_PER_CPU(int, prof_counter
);
59 extern spinlock_t ipi_lock
[];
61 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
62 /* Function Prototypes */
63 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
65 void smp_reschedule_interrupt(void);
66 void smp_flush_cache_all_interrupt(void);
68 static void flush_tlb_all_ipi(void *);
69 static void flush_tlb_others(cpumask_t
, struct mm_struct
*,
70 struct vm_area_struct
*, unsigned long);
72 void smp_invalidate_interrupt(void);
74 static void stop_this_cpu(void *);
76 void smp_ipi_timer_interrupt(struct pt_regs
*);
77 void smp_local_timer_interrupt(void);
79 static void send_IPI_allbutself(int, int);
80 static void send_IPI_mask(const struct cpumask
*, int, int);
82 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
83 /* Rescheduling request Routines */
84 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
86 /*==========================================================================*
87 * Name: smp_send_reschedule
89 * Description: This routine requests other CPU to execute rescheduling.
90 * 1.Send 'RESCHEDULE_IPI' to other CPU.
91 * Request other CPU to execute 'smp_reschedule_interrupt()'.
93 * Born on Date: 2002.02.05
95 * Arguments: cpu_id - Target CPU ID
97 * Returns: void (cannot fail)
100 * Date Who Description
101 * ---------- --- --------------------------------------------------------
103 *==========================================================================*/
104 void smp_send_reschedule(int cpu_id
)
106 WARN_ON(cpu_is_offline(cpu_id
));
107 send_IPI_mask(cpumask_of(cpu_id
), RESCHEDULE_IPI
, 1);
110 /*==========================================================================*
111 * Name: smp_reschedule_interrupt
113 * Description: This routine executes on CPU which received
116 * Born on Date: 2002.02.05
120 * Returns: void (cannot fail)
123 * Date Who Description
124 * ---------- --- --------------------------------------------------------
126 *==========================================================================*/
127 void smp_reschedule_interrupt(void)
132 /*==========================================================================*
133 * Name: smp_flush_cache_all
135 * Description: This routine sends a 'INVALIDATE_CACHE_IPI' to all other
136 * CPUs in the system.
138 * Born on Date: 2003-05-28
142 * Returns: void (cannot fail)
145 * Date Who Description
146 * ---------- --- --------------------------------------------------------
148 *==========================================================================*/
149 void smp_flush_cache_all(void)
155 cpumask_copy(&cpumask
, cpu_online_mask
);
156 cpumask_clear_cpu(smp_processor_id(), &cpumask
);
157 spin_lock(&flushcache_lock
);
158 mask
=cpumask_bits(&cpumask
);
159 atomic_or(*mask
, (atomic_t
*)&flushcache_cpumask
);
160 send_IPI_mask(&cpumask
, INVALIDATE_CACHE_IPI
, 0);
161 _flush_cache_copyback_all();
162 while (flushcache_cpumask
)
164 spin_unlock(&flushcache_lock
);
167 EXPORT_SYMBOL(smp_flush_cache_all
);
169 void smp_flush_cache_all_interrupt(void)
171 _flush_cache_copyback_all();
172 clear_bit(smp_processor_id(), &flushcache_cpumask
);
175 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
176 /* TLB flush request Routines */
177 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
179 /*==========================================================================*
180 * Name: smp_flush_tlb_all
182 * Description: This routine flushes all processes TLBs.
183 * 1.Request other CPU to execute 'flush_tlb_all_ipi()'.
184 * 2.Execute 'do_flush_tlb_all_local()'.
186 * Born on Date: 2002.02.05
190 * Returns: void (cannot fail)
193 * Date Who Description
194 * ---------- --- --------------------------------------------------------
196 *==========================================================================*/
197 void smp_flush_tlb_all(void)
202 local_irq_save(flags
);
204 local_irq_restore(flags
);
205 smp_call_function(flush_tlb_all_ipi
, NULL
, 1);
209 /*==========================================================================*
210 * Name: flush_tlb_all_ipi
212 * Description: This routine flushes all local TLBs.
213 * 1.Execute 'do_flush_tlb_all_local()'.
215 * Born on Date: 2002.02.05
217 * Arguments: *info - not used
219 * Returns: void (cannot fail)
222 * Date Who Description
223 * ---------- --- --------------------------------------------------------
225 *==========================================================================*/
226 static void flush_tlb_all_ipi(void *info
)
231 /*==========================================================================*
232 * Name: smp_flush_tlb_mm
234 * Description: This routine flushes the specified mm context TLB's.
236 * Born on Date: 2002.02.05
238 * Arguments: *mm - a pointer to the mm struct for flush TLB
240 * Returns: void (cannot fail)
243 * Date Who Description
244 * ---------- --- --------------------------------------------------------
246 *==========================================================================*/
247 void smp_flush_tlb_mm(struct mm_struct
*mm
)
255 cpu_id
= smp_processor_id();
256 mmc
= &mm
->context
[cpu_id
];
257 cpumask_copy(&cpu_mask
, mm_cpumask(mm
));
258 cpumask_clear_cpu(cpu_id
, &cpu_mask
);
260 if (*mmc
!= NO_CONTEXT
) {
261 local_irq_save(flags
);
263 if (mm
== current
->mm
)
264 activate_context(mm
);
266 cpumask_clear_cpu(cpu_id
, mm_cpumask(mm
));
267 local_irq_restore(flags
);
269 if (!cpumask_empty(&cpu_mask
))
270 flush_tlb_others(cpu_mask
, mm
, NULL
, FLUSH_ALL
);
275 /*==========================================================================*
276 * Name: smp_flush_tlb_range
278 * Description: This routine flushes a range of pages.
280 * Born on Date: 2002.02.05
282 * Arguments: *mm - a pointer to the mm struct for flush TLB
286 * Returns: void (cannot fail)
289 * Date Who Description
290 * ---------- --- --------------------------------------------------------
292 *==========================================================================*/
293 void smp_flush_tlb_range(struct vm_area_struct
*vma
, unsigned long start
,
296 smp_flush_tlb_mm(vma
->vm_mm
);
299 /*==========================================================================*
300 * Name: smp_flush_tlb_page
302 * Description: This routine flushes one page.
304 * Born on Date: 2002.02.05
306 * Arguments: *vma - a pointer to the vma struct include va
307 * va - virtual address for flush TLB
309 * Returns: void (cannot fail)
312 * Date Who Description
313 * ---------- --- --------------------------------------------------------
315 *==========================================================================*/
316 void smp_flush_tlb_page(struct vm_area_struct
*vma
, unsigned long va
)
318 struct mm_struct
*mm
= vma
->vm_mm
;
325 cpu_id
= smp_processor_id();
326 mmc
= &mm
->context
[cpu_id
];
327 cpumask_copy(&cpu_mask
, mm_cpumask(mm
));
328 cpumask_clear_cpu(cpu_id
, &cpu_mask
);
335 if (*mmc
!= NO_CONTEXT
) {
336 local_irq_save(flags
);
338 va
|= (*mmc
& MMU_CONTEXT_ASID_MASK
);
339 __flush_tlb_page(va
);
340 local_irq_restore(flags
);
342 if (!cpumask_empty(&cpu_mask
))
343 flush_tlb_others(cpu_mask
, mm
, vma
, va
);
348 /*==========================================================================*
349 * Name: flush_tlb_others
351 * Description: This routine requests other CPU to execute flush TLB.
352 * 1.Setup parameters.
353 * 2.Send 'INVALIDATE_TLB_IPI' to other CPU.
354 * Request other CPU to execute 'smp_invalidate_interrupt()'.
355 * 3.Wait for other CPUs operation finished.
357 * Born on Date: 2002.02.05
359 * Arguments: cpumask - bitmap of target CPUs
360 * *mm - a pointer to the mm struct for flush TLB
361 * *vma - a pointer to the vma struct include va
362 * va - virtual address for flush TLB
364 * Returns: void (cannot fail)
367 * Date Who Description
368 * ---------- --- --------------------------------------------------------
370 *==========================================================================*/
371 static void flush_tlb_others(cpumask_t cpumask
, struct mm_struct
*mm
,
372 struct vm_area_struct
*vma
, unsigned long va
)
378 if (!(flags
& 0x0040)) /* Interrupt Disable NONONO */
380 #endif /* DEBUG_SMP */
383 * A couple of (to be removed) sanity checks:
385 * - we do not send IPIs to not-yet booted CPUs.
386 * - current CPU must not be in mask
387 * - mask must exist :)
389 BUG_ON(cpumask_empty(&cpumask
));
391 BUG_ON(cpumask_test_cpu(smp_processor_id(), &cpumask
));
394 /* If a CPU which we ran on has gone down, OK. */
395 cpumask_and(&cpumask
, &cpumask
, cpu_online_mask
);
396 if (cpumask_empty(&cpumask
))
400 * i'm not happy about this global shared spinlock in the
401 * MM hot path, but we'll see how contended it is.
402 * Temporarily this turns IRQs off, so that lockups are
403 * detected by the NMI watchdog.
405 spin_lock(&tlbstate_lock
);
410 mask
=cpumask_bits(&cpumask
);
411 atomic_or(*mask
, (atomic_t
*)&flush_cpumask
);
414 * We have to send the IPI only to
417 send_IPI_mask(&cpumask
, INVALIDATE_TLB_IPI
, 0);
419 while (!cpumask_empty(&flush_cpumask
)) {
420 /* nothing. lockup detection does not belong here */
427 spin_unlock(&tlbstate_lock
);
430 /*==========================================================================*
431 * Name: smp_invalidate_interrupt
433 * Description: This routine executes on CPU which received
434 * 'INVALIDATE_TLB_IPI'.
436 * 2.Report flush TLB process was finished.
438 * Born on Date: 2002.02.05
442 * Returns: void (cannot fail)
445 * Date Who Description
446 * ---------- --- --------------------------------------------------------
448 *==========================================================================*/
449 void smp_invalidate_interrupt(void)
451 int cpu_id
= smp_processor_id();
452 unsigned long *mmc
= &flush_mm
->context
[cpu_id
];
454 if (!cpumask_test_cpu(cpu_id
, &flush_cpumask
))
457 if (flush_va
== FLUSH_ALL
) {
459 if (flush_mm
== current
->active_mm
)
460 activate_context(flush_mm
);
462 cpumask_clear_cpu(cpu_id
, mm_cpumask(flush_mm
));
464 unsigned long va
= flush_va
;
466 if (*mmc
!= NO_CONTEXT
) {
468 va
|= (*mmc
& MMU_CONTEXT_ASID_MASK
);
469 __flush_tlb_page(va
);
472 cpumask_clear_cpu(cpu_id
, &flush_cpumask
);
475 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
476 /* Stop CPU request Routines */
477 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
479 /*==========================================================================*
480 * Name: smp_send_stop
482 * Description: This routine requests stop all CPUs.
483 * 1.Request other CPU to execute 'stop_this_cpu()'.
485 * Born on Date: 2002.02.05
489 * Returns: void (cannot fail)
492 * Date Who Description
493 * ---------- --- --------------------------------------------------------
495 *==========================================================================*/
496 void smp_send_stop(void)
498 smp_call_function(stop_this_cpu
, NULL
, 0);
501 /*==========================================================================*
502 * Name: stop_this_cpu
504 * Description: This routine halt CPU.
506 * Born on Date: 2002.02.05
510 * Returns: void (cannot fail)
513 * Date Who Description
514 * ---------- --- --------------------------------------------------------
516 *==========================================================================*/
517 static void stop_this_cpu(void *dummy
)
519 int cpu_id
= smp_processor_id();
524 set_cpu_online(cpu_id
, false);
532 outl(0, M32R_ICU_IMASK_PORTL
);
533 inl(M32R_ICU_IMASK_PORTL
); /* dummy read */
539 void arch_send_call_function_ipi_mask(const struct cpumask
*mask
)
541 send_IPI_mask(mask
, CALL_FUNCTION_IPI
, 0);
544 void arch_send_call_function_single_ipi(int cpu
)
546 send_IPI_mask(cpumask_of(cpu
), CALL_FUNC_SINGLE_IPI
, 0);
549 /*==========================================================================*
550 * Name: smp_call_function_interrupt
552 * Description: This routine executes on CPU which received
553 * 'CALL_FUNCTION_IPI'.
555 * Born on Date: 2002.02.05
559 * Returns: void (cannot fail)
562 * Date Who Description
563 * ---------- --- --------------------------------------------------------
565 *==========================================================================*/
566 void smp_call_function_interrupt(void)
569 generic_smp_call_function_interrupt();
573 void smp_call_function_single_interrupt(void)
576 generic_smp_call_function_single_interrupt();
580 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
582 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
584 /*==========================================================================*
585 * Name: smp_send_timer
587 * Description: This routine sends a 'LOCAL_TIMER_IPI' to all other CPUs
590 * Born on Date: 2002.02.05
594 * Returns: void (cannot fail)
597 * Date Who Description
598 * ---------- --- --------------------------------------------------------
600 *==========================================================================*/
601 void smp_send_timer(void)
603 send_IPI_allbutself(LOCAL_TIMER_IPI
, 1);
606 /*==========================================================================*
607 * Name: smp_send_timer
609 * Description: This routine executes on CPU which received
612 * Born on Date: 2002.02.05
614 * Arguments: *regs - a pointer to the saved regster info
616 * Returns: void (cannot fail)
619 * Date Who Description
620 * ---------- --- --------------------------------------------------------
622 *==========================================================================*/
623 void smp_ipi_timer_interrupt(struct pt_regs
*regs
)
625 struct pt_regs
*old_regs
;
626 old_regs
= set_irq_regs(regs
);
628 smp_local_timer_interrupt();
630 set_irq_regs(old_regs
);
633 /*==========================================================================*
634 * Name: smp_local_timer_interrupt
636 * Description: Local timer interrupt handler. It does both profiling and
637 * process statistics/rescheduling.
638 * We do profiling in every local tick, statistics/rescheduling
639 * happen only every 'profiling multiplier' ticks. The default
640 * multiplier is 1 and it can be changed by writing the new
641 * multiplier value into /proc/profile.
643 * Born on Date: 2002.02.05
645 * Arguments: *regs - a pointer to the saved regster info
647 * Returns: void (cannot fail)
649 * Original: arch/i386/kernel/apic.c
652 * Date Who Description
653 * ---------- --- --------------------------------------------------------
654 * 2003-06-24 hy use per_cpu structure.
655 *==========================================================================*/
656 void smp_local_timer_interrupt(void)
658 int user
= user_mode(get_irq_regs());
659 int cpu_id
= smp_processor_id();
662 * The profiling function is SMP safe. (nothing can mess
663 * around with "current", and the profiling counters are
664 * updated with atomic operations). This is especially
665 * useful with a profiling multiplier != 1
668 profile_tick(CPU_PROFILING
);
670 if (--per_cpu(prof_counter
, cpu_id
) <= 0) {
672 * The multiplier may have changed since the last time we got
673 * to this point as a result of the user writing to
674 * /proc/profile. In this case we need to adjust the APIC
677 * Interrupts are already masked off at this point.
679 per_cpu(prof_counter
, cpu_id
)
680 = per_cpu(prof_multiplier
, cpu_id
);
681 if (per_cpu(prof_counter
, cpu_id
)
682 != per_cpu(prof_old_multiplier
, cpu_id
))
684 per_cpu(prof_old_multiplier
, cpu_id
)
685 = per_cpu(prof_counter
, cpu_id
);
688 update_process_times(user
);
692 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
693 /* Send IPI Routines */
694 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
696 /*==========================================================================*
697 * Name: send_IPI_allbutself
699 * Description: This routine sends a IPI to all other CPUs in the system.
701 * Born on Date: 2002.02.05
703 * Arguments: ipi_num - Number of IPI
704 * try - 0 : Send IPI certainly.
705 * !0 : The following IPI is not sent when Target CPU
706 * has not received the before IPI.
708 * Returns: void (cannot fail)
711 * Date Who Description
712 * ---------- --- --------------------------------------------------------
714 *==========================================================================*/
715 static void send_IPI_allbutself(int ipi_num
, int try)
719 cpumask_copy(&cpumask
, cpu_online_mask
);
720 cpumask_clear_cpu(smp_processor_id(), &cpumask
);
722 send_IPI_mask(&cpumask
, ipi_num
, try);
725 /*==========================================================================*
726 * Name: send_IPI_mask
728 * Description: This routine sends a IPI to CPUs in the system.
730 * Born on Date: 2002.02.05
732 * Arguments: cpu_mask - Bitmap of target CPUs logical ID
733 * ipi_num - Number of IPI
734 * try - 0 : Send IPI certainly.
735 * !0 : The following IPI is not sent when Target CPU
736 * has not received the before IPI.
738 * Returns: void (cannot fail)
741 * Date Who Description
742 * ---------- --- --------------------------------------------------------
744 *==========================================================================*/
745 static void send_IPI_mask(const struct cpumask
*cpumask
, int ipi_num
, int try)
747 cpumask_t physid_mask
, tmp
;
749 int num_cpus
= num_online_cpus();
751 if (num_cpus
<= 1) /* NO MP */
754 cpumask_and(&tmp
, cpumask
, cpu_online_mask
);
755 BUG_ON(!cpumask_equal(cpumask
, &tmp
));
757 cpumask_clear(&physid_mask
);
758 for_each_cpu(cpu_id
, cpumask
) {
759 if ((phys_id
= cpu_to_physid(cpu_id
)) != -1)
760 cpumask_set_cpu(phys_id
, &physid_mask
);
763 send_IPI_mask_phys(&physid_mask
, ipi_num
, try);
766 /*==========================================================================*
767 * Name: send_IPI_mask_phys
769 * Description: This routine sends a IPI to other CPUs in the system.
771 * Born on Date: 2002.02.05
773 * Arguments: cpu_mask - Bitmap of target CPUs physical ID
774 * ipi_num - Number of IPI
775 * try - 0 : Send IPI certainly.
776 * !0 : The following IPI is not sent when Target CPU
777 * has not received the before IPI.
779 * Returns: IPICRi regster value.
782 * Date Who Description
783 * ---------- --- --------------------------------------------------------
785 *==========================================================================*/
786 unsigned long send_IPI_mask_phys(const cpumask_t
*physid_mask
, int ipi_num
,
790 volatile unsigned long *ipicr_addr
;
791 unsigned long ipicr_val
;
792 unsigned long my_physid_mask
;
793 unsigned long mask
= cpumask_bits(physid_mask
)[0];
796 if (mask
& ~physids_coerce(phys_cpu_present_map
))
798 if (ipi_num
>= NR_IPIS
|| ipi_num
< 0)
802 ipilock
= &ipi_lock
[ipi_num
];
803 ipicr_addr
= (volatile unsigned long *)(M32R_ICU_IPICR_ADDR
805 my_physid_mask
= ~(1 << smp_processor_id());
810 * write IPICRi (send IPIi)
814 __asm__
__volatile__ (
815 ";; CHECK IPICRi == 0 \n\t"
823 ";; WRITE IPICRi (send IPIi) \n\t"
830 : "r"(ipicr_addr
), "r"(mask
), "r"(try), "r"(my_physid_mask
)
833 spin_unlock(ipilock
);