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);
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_id
), RESCHEDULE_IPI
, 1);
119 /*==========================================================================*
120 * Name: smp_reschedule_interrupt
122 * Description: This routine executes on CPU which received
125 * Born on Date: 2002.02.05
129 * Returns: void (cannot fail)
132 * Date Who Description
133 * ---------- --- --------------------------------------------------------
135 *==========================================================================*/
136 void smp_reschedule_interrupt(void)
141 /*==========================================================================*
142 * Name: smp_flush_cache_all
144 * Description: This routine sends a 'INVALIDATE_CACHE_IPI' to all other
145 * CPUs in the system.
147 * Born on Date: 2003-05-28
151 * Returns: void (cannot fail)
154 * Date Who Description
155 * ---------- --- --------------------------------------------------------
157 *==========================================================================*/
158 void smp_flush_cache_all(void)
164 cpumask_copy(&cpumask
, cpu_online_mask
);
165 cpumask_clear_cpu(smp_processor_id(), &cpumask
);
166 spin_lock(&flushcache_lock
);
167 mask
=cpumask_bits(&cpumask
);
168 atomic_set_mask(*mask
, (atomic_t
*)&flushcache_cpumask
);
169 send_IPI_mask(&cpumask
, INVALIDATE_CACHE_IPI
, 0);
170 _flush_cache_copyback_all();
171 while (flushcache_cpumask
)
173 spin_unlock(&flushcache_lock
);
177 void smp_flush_cache_all_interrupt(void)
179 _flush_cache_copyback_all();
180 clear_bit(smp_processor_id(), &flushcache_cpumask
);
183 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
184 /* TLB flush request Routines */
185 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
187 /*==========================================================================*
188 * Name: smp_flush_tlb_all
190 * Description: This routine flushes all processes TLBs.
191 * 1.Request other CPU to execute 'flush_tlb_all_ipi()'.
192 * 2.Execute 'do_flush_tlb_all_local()'.
194 * Born on Date: 2002.02.05
198 * Returns: void (cannot fail)
201 * Date Who Description
202 * ---------- --- --------------------------------------------------------
204 *==========================================================================*/
205 void smp_flush_tlb_all(void)
210 local_irq_save(flags
);
212 local_irq_restore(flags
);
213 smp_call_function(flush_tlb_all_ipi
, NULL
, 1);
217 /*==========================================================================*
218 * Name: flush_tlb_all_ipi
220 * Description: This routine flushes all local TLBs.
221 * 1.Execute 'do_flush_tlb_all_local()'.
223 * Born on Date: 2002.02.05
225 * Arguments: *info - not used
227 * Returns: void (cannot fail)
230 * Date Who Description
231 * ---------- --- --------------------------------------------------------
233 *==========================================================================*/
234 static void flush_tlb_all_ipi(void *info
)
239 /*==========================================================================*
240 * Name: smp_flush_tlb_mm
242 * Description: This routine flushes the specified mm context TLB's.
244 * Born on Date: 2002.02.05
246 * Arguments: *mm - a pointer to the mm struct for flush TLB
248 * Returns: void (cannot fail)
251 * Date Who Description
252 * ---------- --- --------------------------------------------------------
254 *==========================================================================*/
255 void smp_flush_tlb_mm(struct mm_struct
*mm
)
263 cpu_id
= smp_processor_id();
264 mmc
= &mm
->context
[cpu_id
];
265 cpumask_copy(&cpu_mask
, mm_cpumask(mm
));
266 cpumask_clear_cpu(cpu_id
, &cpu_mask
);
268 if (*mmc
!= NO_CONTEXT
) {
269 local_irq_save(flags
);
271 if (mm
== current
->mm
)
272 activate_context(mm
);
274 cpumask_clear_cpu(cpu_id
, mm_cpumask(mm
));
275 local_irq_restore(flags
);
277 if (!cpumask_empty(&cpu_mask
))
278 flush_tlb_others(cpu_mask
, mm
, NULL
, FLUSH_ALL
);
283 /*==========================================================================*
284 * Name: smp_flush_tlb_range
286 * Description: This routine flushes a range of pages.
288 * Born on Date: 2002.02.05
290 * Arguments: *mm - a pointer to the mm struct for flush TLB
294 * Returns: void (cannot fail)
297 * Date Who Description
298 * ---------- --- --------------------------------------------------------
300 *==========================================================================*/
301 void smp_flush_tlb_range(struct vm_area_struct
*vma
, unsigned long start
,
304 smp_flush_tlb_mm(vma
->vm_mm
);
307 /*==========================================================================*
308 * Name: smp_flush_tlb_page
310 * Description: This routine flushes one page.
312 * Born on Date: 2002.02.05
314 * Arguments: *vma - a pointer to the vma struct include va
315 * va - virtual address for flush TLB
317 * Returns: void (cannot fail)
320 * Date Who Description
321 * ---------- --- --------------------------------------------------------
323 *==========================================================================*/
324 void smp_flush_tlb_page(struct vm_area_struct
*vma
, unsigned long va
)
326 struct mm_struct
*mm
= vma
->vm_mm
;
333 cpu_id
= smp_processor_id();
334 mmc
= &mm
->context
[cpu_id
];
335 cpumask_copy(&cpu_mask
, mm_cpumask(mm
));
336 cpumask_clear_cpu(cpu_id
, &cpu_mask
);
343 if (*mmc
!= NO_CONTEXT
) {
344 local_irq_save(flags
);
346 va
|= (*mmc
& MMU_CONTEXT_ASID_MASK
);
347 __flush_tlb_page(va
);
348 local_irq_restore(flags
);
350 if (!cpumask_empty(&cpu_mask
))
351 flush_tlb_others(cpu_mask
, mm
, vma
, va
);
356 /*==========================================================================*
357 * Name: flush_tlb_others
359 * Description: This routine requests other CPU to execute flush TLB.
360 * 1.Setup parameters.
361 * 2.Send 'INVALIDATE_TLB_IPI' to other CPU.
362 * Request other CPU to execute 'smp_invalidate_interrupt()'.
363 * 3.Wait for other CPUs operation finished.
365 * Born on Date: 2002.02.05
367 * Arguments: cpumask - bitmap of target CPUs
368 * *mm - a pointer to the mm struct for flush TLB
369 * *vma - a pointer to the vma struct include va
370 * va - virtual address for flush TLB
372 * Returns: void (cannot fail)
375 * Date Who Description
376 * ---------- --- --------------------------------------------------------
378 *==========================================================================*/
379 static void flush_tlb_others(cpumask_t cpumask
, struct mm_struct
*mm
,
380 struct vm_area_struct
*vma
, unsigned long va
)
386 if (!(flags
& 0x0040)) /* Interrupt Disable NONONO */
388 #endif /* DEBUG_SMP */
391 * A couple of (to be removed) sanity checks:
393 * - we do not send IPIs to not-yet booted CPUs.
394 * - current CPU must not be in mask
395 * - mask must exist :)
397 BUG_ON(cpumask_empty(&cpumask
));
399 BUG_ON(cpumask_test_cpu(smp_processor_id(), &cpumask
));
402 /* If a CPU which we ran on has gone down, OK. */
403 cpumask_and(&cpumask
, &cpumask
, cpu_online_mask
);
404 if (cpumask_empty(&cpumask
))
408 * i'm not happy about this global shared spinlock in the
409 * MM hot path, but we'll see how contended it is.
410 * Temporarily this turns IRQs off, so that lockups are
411 * detected by the NMI watchdog.
413 spin_lock(&tlbstate_lock
);
418 mask
=cpumask_bits(&cpumask
);
419 atomic_set_mask(*mask
, (atomic_t
*)&flush_cpumask
);
422 * We have to send the IPI only to
425 send_IPI_mask(&cpumask
, INVALIDATE_TLB_IPI
, 0);
427 while (!cpumask_empty((cpumask_t
*)&flush_cpumask
)) {
428 /* nothing. lockup detection does not belong here */
435 spin_unlock(&tlbstate_lock
);
438 /*==========================================================================*
439 * Name: smp_invalidate_interrupt
441 * Description: This routine executes on CPU which received
442 * 'INVALIDATE_TLB_IPI'.
444 * 2.Report flush TLB process was finished.
446 * Born on Date: 2002.02.05
450 * Returns: void (cannot fail)
453 * Date Who Description
454 * ---------- --- --------------------------------------------------------
456 *==========================================================================*/
457 void smp_invalidate_interrupt(void)
459 int cpu_id
= smp_processor_id();
460 unsigned long *mmc
= &flush_mm
->context
[cpu_id
];
462 if (!cpumask_test_cpu(cpu_id
, &flush_cpumask
))
465 if (flush_va
== FLUSH_ALL
) {
467 if (flush_mm
== current
->active_mm
)
468 activate_context(flush_mm
);
470 cpumask_clear_cpu(cpu_id
, mm_cpumask(flush_mm
));
472 unsigned long va
= flush_va
;
474 if (*mmc
!= NO_CONTEXT
) {
476 va
|= (*mmc
& MMU_CONTEXT_ASID_MASK
);
477 __flush_tlb_page(va
);
480 cpumask_clear_cpu(cpu_id
, (cpumask_t
*)&flush_cpumask
);
483 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
484 /* Stop CPU request Routines */
485 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
487 /*==========================================================================*
488 * Name: smp_send_stop
490 * Description: This routine requests stop all CPUs.
491 * 1.Request other CPU to execute 'stop_this_cpu()'.
493 * Born on Date: 2002.02.05
497 * Returns: void (cannot fail)
500 * Date Who Description
501 * ---------- --- --------------------------------------------------------
503 *==========================================================================*/
504 void smp_send_stop(void)
506 smp_call_function(stop_this_cpu
, NULL
, 0);
509 /*==========================================================================*
510 * Name: stop_this_cpu
512 * Description: This routine halt CPU.
514 * Born on Date: 2002.02.05
518 * Returns: void (cannot fail)
521 * Date Who Description
522 * ---------- --- --------------------------------------------------------
524 *==========================================================================*/
525 static void stop_this_cpu(void *dummy
)
527 int cpu_id
= smp_processor_id();
532 set_cpu_online(cpu_id
, false);
540 outl(0, M32R_ICU_IMASK_PORTL
);
541 inl(M32R_ICU_IMASK_PORTL
); /* dummy read */
547 void arch_send_call_function_ipi_mask(const struct cpumask
*mask
)
549 send_IPI_mask(mask
, CALL_FUNCTION_IPI
, 0);
552 void arch_send_call_function_single_ipi(int cpu
)
554 send_IPI_mask(cpumask_of(cpu
), CALL_FUNC_SINGLE_IPI
, 0);
557 /*==========================================================================*
558 * Name: smp_call_function_interrupt
560 * Description: This routine executes on CPU which received
561 * 'CALL_FUNCTION_IPI'.
563 * Born on Date: 2002.02.05
567 * Returns: void (cannot fail)
570 * Date Who Description
571 * ---------- --- --------------------------------------------------------
573 *==========================================================================*/
574 void smp_call_function_interrupt(void)
577 generic_smp_call_function_interrupt();
581 void smp_call_function_single_interrupt(void)
584 generic_smp_call_function_single_interrupt();
588 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
590 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
592 /*==========================================================================*
593 * Name: smp_send_timer
595 * Description: This routine sends a 'LOCAL_TIMER_IPI' to all other CPUs
598 * Born on Date: 2002.02.05
602 * Returns: void (cannot fail)
605 * Date Who Description
606 * ---------- --- --------------------------------------------------------
608 *==========================================================================*/
609 void smp_send_timer(void)
611 send_IPI_allbutself(LOCAL_TIMER_IPI
, 1);
614 /*==========================================================================*
615 * Name: smp_send_timer
617 * Description: This routine executes on CPU which received
620 * Born on Date: 2002.02.05
622 * Arguments: *regs - a pointer to the saved regster info
624 * Returns: void (cannot fail)
627 * Date Who Description
628 * ---------- --- --------------------------------------------------------
630 *==========================================================================*/
631 void smp_ipi_timer_interrupt(struct pt_regs
*regs
)
633 struct pt_regs
*old_regs
;
634 old_regs
= set_irq_regs(regs
);
636 smp_local_timer_interrupt();
638 set_irq_regs(old_regs
);
641 /*==========================================================================*
642 * Name: smp_local_timer_interrupt
644 * Description: Local timer interrupt handler. It does both profiling and
645 * process statistics/rescheduling.
646 * We do profiling in every local tick, statistics/rescheduling
647 * happen only every 'profiling multiplier' ticks. The default
648 * multiplier is 1 and it can be changed by writing the new
649 * multiplier value into /proc/profile.
651 * Born on Date: 2002.02.05
653 * Arguments: *regs - a pointer to the saved regster info
655 * Returns: void (cannot fail)
657 * Original: arch/i386/kernel/apic.c
660 * Date Who Description
661 * ---------- --- --------------------------------------------------------
662 * 2003-06-24 hy use per_cpu structure.
663 *==========================================================================*/
664 void smp_local_timer_interrupt(void)
666 int user
= user_mode(get_irq_regs());
667 int cpu_id
= smp_processor_id();
670 * The profiling function is SMP safe. (nothing can mess
671 * around with "current", and the profiling counters are
672 * updated with atomic operations). This is especially
673 * useful with a profiling multiplier != 1
676 profile_tick(CPU_PROFILING
);
678 if (--per_cpu(prof_counter
, cpu_id
) <= 0) {
680 * The multiplier may have changed since the last time we got
681 * to this point as a result of the user writing to
682 * /proc/profile. In this case we need to adjust the APIC
685 * Interrupts are already masked off at this point.
687 per_cpu(prof_counter
, cpu_id
)
688 = per_cpu(prof_multiplier
, cpu_id
);
689 if (per_cpu(prof_counter
, cpu_id
)
690 != per_cpu(prof_old_multiplier
, cpu_id
))
692 per_cpu(prof_old_multiplier
, cpu_id
)
693 = per_cpu(prof_counter
, cpu_id
);
696 update_process_times(user
);
700 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
701 /* Send IPI Routines */
702 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
704 /*==========================================================================*
705 * Name: send_IPI_allbutself
707 * Description: This routine sends a IPI to all other CPUs in the system.
709 * Born on Date: 2002.02.05
711 * Arguments: ipi_num - Number of IPI
712 * try - 0 : Send IPI certainly.
713 * !0 : The following IPI is not sent when Target CPU
714 * has not received the before IPI.
716 * Returns: void (cannot fail)
719 * Date Who Description
720 * ---------- --- --------------------------------------------------------
722 *==========================================================================*/
723 static void send_IPI_allbutself(int ipi_num
, int try)
727 cpumask_copy(&cpumask
, cpu_online_mask
);
728 cpumask_clear_cpu(smp_processor_id(), &cpumask
);
730 send_IPI_mask(&cpumask
, ipi_num
, try);
733 /*==========================================================================*
734 * Name: send_IPI_mask
736 * Description: This routine sends a IPI to CPUs in the system.
738 * Born on Date: 2002.02.05
740 * Arguments: cpu_mask - Bitmap of target CPUs logical ID
741 * ipi_num - Number of IPI
742 * try - 0 : Send IPI certainly.
743 * !0 : The following IPI is not sent when Target CPU
744 * has not received the before IPI.
746 * Returns: void (cannot fail)
749 * Date Who Description
750 * ---------- --- --------------------------------------------------------
752 *==========================================================================*/
753 static void send_IPI_mask(const struct cpumask
*cpumask
, int ipi_num
, int try)
755 cpumask_t physid_mask
, tmp
;
757 int num_cpus
= num_online_cpus();
759 if (num_cpus
<= 1) /* NO MP */
762 cpumask_and(&tmp
, cpumask
, cpu_online_mask
);
763 BUG_ON(!cpumask_equal(cpumask
, &tmp
));
765 cpumask_clear(&physid_mask
);
766 for_each_cpu(cpu_id
, cpumask
) {
767 if ((phys_id
= cpu_to_physid(cpu_id
)) != -1)
768 cpumask_set_cpu(phys_id
, &physid_mask
);
771 send_IPI_mask_phys(&physid_mask
, ipi_num
, try);
774 /*==========================================================================*
775 * Name: send_IPI_mask_phys
777 * Description: This routine sends a IPI to other CPUs in the system.
779 * Born on Date: 2002.02.05
781 * Arguments: cpu_mask - Bitmap of target CPUs physical ID
782 * ipi_num - Number of IPI
783 * try - 0 : Send IPI certainly.
784 * !0 : The following IPI is not sent when Target CPU
785 * has not received the before IPI.
787 * Returns: IPICRi regster value.
790 * Date Who Description
791 * ---------- --- --------------------------------------------------------
793 *==========================================================================*/
794 unsigned long send_IPI_mask_phys(const cpumask_t
*physid_mask
, int ipi_num
,
798 volatile unsigned long *ipicr_addr
;
799 unsigned long ipicr_val
;
800 unsigned long my_physid_mask
;
801 unsigned long mask
= cpumask_bits(physid_mask
)[0];
804 if (mask
& ~physids_coerce(phys_cpu_present_map
))
806 if (ipi_num
>= NR_IPIS
|| ipi_num
< 0)
810 ipilock
= &ipi_lock
[ipi_num
];
811 ipicr_addr
= (volatile unsigned long *)(M32R_ICU_IPICR_ADDR
813 my_physid_mask
= ~(1 << smp_processor_id());
818 * write IPICRi (send IPIi)
822 __asm__
__volatile__ (
823 ";; CHECK IPICRi == 0 \n\t"
831 ";; WRITE IPICRi (send IPIi) \n\t"
838 : "r"(ipicr_addr
), "r"(mask
), "r"(try), "r"(my_physid_mask
)
841 spin_unlock(ipilock
);