summaryrefslogtreecommitdiff
path: root/arch/m32r/kernel/smp.c
blob: 48b187f2d2b350d5cd5da50ba71e1a9b8664a8c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
/*
 *  linux/arch/m32r/kernel/smp.c
 *
 *  M32R SMP support routines.
 *
 *  Copyright (c) 2001, 2002  Hitoshi Yamamoto
 *
 *  Taken from i386 version.
 *    (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
 *    (c) 1998-99, 2000 Ingo Molnar <mingo@redhat.com>
 *
 *  This code is released under the GNU General Public License version 2 or
 *  later.
 */

#undef DEBUG_SMP

#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/mm.h>
#include <linux/smp.h>
#include <linux/profile.h>
#include <linux/cpu.h>

#include <asm/cacheflush.h>
#include <asm/pgalloc.h>
#include <asm/atomic.h>
#include <asm/io.h>
#include <asm/mmu_context.h>
#include <asm/m32r.h>

/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
/* Data structures and variables                                             */
/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/

/*
 * Structure and data for smp_call_function(). This is designed to minimise
 * static memory requirements. It also looks cleaner.
 */
static DEFINE_SPINLOCK(call_lock);

struct call_data_struct {
	void (*func) (void *info);
	void *info;
	atomic_t started;
	atomic_t finished;
	int wait;
} __attribute__ ((__aligned__(SMP_CACHE_BYTES)));

static struct call_data_struct *call_data;

/*
 * For flush_cache_all()
 */
static DEFINE_SPINLOCK(flushcache_lock);
static volatile unsigned long flushcache_cpumask = 0;

/*
 * For flush_tlb_others()
 */
static volatile cpumask_t flush_cpumask;
static struct mm_struct *flush_mm;
static struct vm_area_struct *flush_vma;
static volatile unsigned long flush_va;
static DEFINE_SPINLOCK(tlbstate_lock);
#define FLUSH_ALL 0xffffffff

DECLARE_PER_CPU(int, prof_multiplier);
DECLARE_PER_CPU(int, prof_old_multiplier);
DECLARE_PER_CPU(int, prof_counter);

extern spinlock_t ipi_lock[];

/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
/* Function Prototypes                                                       */
/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/

void smp_send_reschedule(int);
void smp_reschedule_interrupt(void);

void smp_flush_cache_all(void);
void smp_flush_cache_all_interrupt(void);

void smp_flush_tlb_all(void);
static void flush_tlb_all_ipi(void *);

void smp_flush_tlb_mm(struct mm_struct *);
void smp_flush_tlb_range(struct vm_area_struct *, unsigned long, \
	unsigned long);
void smp_flush_tlb_page(struct vm_area_struct *, unsigned long);
static void flush_tlb_others(cpumask_t, struct mm_struct *,
	struct vm_area_struct *, unsigned long);
void smp_invalidate_interrupt(void);

void smp_send_stop(void);
static void stop_this_cpu(void *);

int smp_call_function(void (*) (void *), void *, int, int);
void smp_call_function_interrupt(void);

void smp_send_timer(void);
void smp_ipi_timer_interrupt(struct pt_regs *);
void smp_local_timer_interrupt(struct pt_regs *);

void send_IPI_allbutself(int, int);
static void send_IPI_mask(cpumask_t, int, int);
unsigned long send_IPI_mask_phys(cpumask_t, int, int);

/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
/* Rescheduling request Routines                                             */
/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/

/*==========================================================================*
 * Name:         smp_send_reschedule
 *
 * Description:  This routine requests other CPU to execute rescheduling.
 *               1.Send 'RESCHEDULE_IPI' to other CPU.
 *                 Request other CPU to execute 'smp_reschedule_interrupt()'.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    cpu_id - Target CPU ID
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
void smp_send_reschedule(int cpu_id)
{
	WARN_ON(cpu_is_offline(cpu_id));
	send_IPI_mask(cpumask_of_cpu(cpu_id), RESCHEDULE_IPI, 1);
}

/*==========================================================================*
 * Name:         smp_reschedule_interrupt
 *
 * Description:  This routine executes on CPU which received
 *               'RESCHEDULE_IPI'.
 *               Rescheduling is processed at the exit of interrupt
 *               operation.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    NONE
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
void smp_reschedule_interrupt(void)
{
	/* nothing to do */
}

/*==========================================================================*
 * Name:         smp_flush_cache_all
 *
 * Description:  This routine sends a 'INVALIDATE_CACHE_IPI' to all other
 *               CPUs in the system.
 *
 * Born on Date: 2003-05-28
 *
 * Arguments:    NONE
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
void smp_flush_cache_all(void)
{
	cpumask_t cpumask;
	unsigned long *mask;

	preempt_disable();
	cpumask = cpu_online_map;
	cpu_clear(smp_processor_id(), cpumask);
	spin_lock(&flushcache_lock);
	mask=cpus_addr(cpumask);
	atomic_set_mask(*mask, (atomic_t *)&flushcache_cpumask);
	send_IPI_mask(cpumask, INVALIDATE_CACHE_IPI, 0);
	_flush_cache_copyback_all();
	while (flushcache_cpumask)
		mb();
	spin_unlock(&flushcache_lock);
	preempt_enable();
}

void smp_flush_cache_all_interrupt(void)
{
	_flush_cache_copyback_all();
	clear_bit(smp_processor_id(), &flushcache_cpumask);
}

/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
/* TLB flush request Routins                                                 */
/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/

/*==========================================================================*
 * Name:         smp_flush_tlb_all
 *
 * Description:  This routine flushes all processes TLBs.
 *               1.Request other CPU to execute 'flush_tlb_all_ipi()'.
 *               2.Execute 'do_flush_tlb_all_local()'.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    NONE
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
void smp_flush_tlb_all(void)
{
	unsigned long flags;

	preempt_disable();
	local_irq_save(flags);
	__flush_tlb_all();
	local_irq_restore(flags);
	smp_call_function(flush_tlb_all_ipi, 0, 1, 1);
	preempt_enable();
}

/*==========================================================================*
 * Name:         flush_tlb_all_ipi
 *
 * Description:  This routine flushes all local TLBs.
 *               1.Execute 'do_flush_tlb_all_local()'.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    *info - not used
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
static void flush_tlb_all_ipi(void *info)
{
	__flush_tlb_all();
}

/*==========================================================================*
 * Name:         smp_flush_tlb_mm
 *
 * Description:  This routine flushes the specified mm context TLB's.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    *mm - a pointer to the mm struct for flush TLB
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
void smp_flush_tlb_mm(struct mm_struct *mm)
{
	int cpu_id = smp_processor_id();
	cpumask_t cpu_mask;
	unsigned long *mmc = &mm->context[cpu_id];
	unsigned long flags;

	preempt_disable();
	cpu_mask = mm->cpu_vm_mask;
	cpu_clear(cpu_id, cpu_mask);

	if (*mmc != NO_CONTEXT) {
		local_irq_save(flags);
		*mmc = NO_CONTEXT;
		if (mm == current->mm)
			activate_context(mm);
		else
			cpu_clear(cpu_id, mm->cpu_vm_mask);
		local_irq_restore(flags);
	}
	if (!cpus_empty(cpu_mask))
		flush_tlb_others(cpu_mask, mm, NULL, FLUSH_ALL);

	preempt_enable();
}

/*==========================================================================*
 * Name:         smp_flush_tlb_range
 *
 * Description:  This routine flushes a range of pages.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    *mm - a pointer to the mm struct for flush TLB
 *               start - not used
 *               end - not used
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
void smp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
	unsigned long end)
{
	smp_flush_tlb_mm(vma->vm_mm);
}

/*==========================================================================*
 * Name:         smp_flush_tlb_page
 *
 * Description:  This routine flushes one page.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    *vma - a pointer to the vma struct include va
 *               va - virtual address for flush TLB
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long va)
{
	struct mm_struct *mm = vma->vm_mm;
	int cpu_id = smp_processor_id();
	cpumask_t cpu_mask;
	unsigned long *mmc = &mm->context[cpu_id];
	unsigned long flags;

	preempt_disable();
	cpu_mask = mm->cpu_vm_mask;
	cpu_clear(cpu_id, cpu_mask);

#ifdef DEBUG_SMP
	if (!mm)
		BUG();
#endif

	if (*mmc != NO_CONTEXT) {
		local_irq_save(flags);
		va &= PAGE_MASK;
		va |= (*mmc & MMU_CONTEXT_ASID_MASK);
		__flush_tlb_page(va);
		local_irq_restore(flags);
	}
	if (!cpus_empty(cpu_mask))
		flush_tlb_others(cpu_mask, mm, vma, va);

	preempt_enable();
}

/*==========================================================================*
 * Name:         flush_tlb_others
 *
 * Description:  This routine requests other CPU to execute flush TLB.
 *               1.Setup parmeters.
 *               2.Send 'INVALIDATE_TLB_IPI' to other CPU.
 *                 Request other CPU to execute 'smp_invalidate_interrupt()'.
 *               3.Wait for other CPUs operation finished.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    cpumask - bitmap of target CPUs
 *               *mm -  a pointer to the mm struct for flush TLB
 *               *vma -  a pointer to the vma struct include va
 *               va - virtual address for flush TLB
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
	struct vm_area_struct *vma, unsigned long va)
{
	unsigned long *mask;
#ifdef DEBUG_SMP
	unsigned long flags;
	__save_flags(flags);
	if (!(flags & 0x0040))	/* Interrupt Disable NONONO */
		BUG();
#endif /* DEBUG_SMP */

	/*
	 * A couple of (to be removed) sanity checks:
	 *
	 * - we do not send IPIs to not-yet booted CPUs.
	 * - current CPU must not be in mask
	 * - mask must exist :)
	 */
	BUG_ON(cpus_empty(cpumask));

	BUG_ON(cpu_isset(smp_processor_id(), cpumask));
	BUG_ON(!mm);

	/* If a CPU which we ran on has gone down, OK. */
	cpus_and(cpumask, cpumask, cpu_online_map);
	if (cpus_empty(cpumask))
		return;

	/*
	 * i'm not happy about this global shared spinlock in the
	 * MM hot path, but we'll see how contended it is.
	 * Temporarily this turns IRQs off, so that lockups are
	 * detected by the NMI watchdog.
	 */
	spin_lock(&tlbstate_lock);

	flush_mm = mm;
	flush_vma = vma;
	flush_va = va;
	mask=cpus_addr(cpumask);
	atomic_set_mask(*mask, (atomic_t *)&flush_cpumask);

	/*
	 * We have to send the IPI only to
	 * CPUs affected.
	 */
	send_IPI_mask(cpumask, INVALIDATE_TLB_IPI, 0);

	while (!cpus_empty(flush_cpumask)) {
		/* nothing. lockup detection does not belong here */
		mb();
	}

	flush_mm = NULL;
	flush_vma = NULL;
	flush_va = 0;
	spin_unlock(&tlbstate_lock);
}

/*==========================================================================*
 * Name:         smp_invalidate_interrupt
 *
 * Description:  This routine executes on CPU which received
 *               'INVALIDATE_TLB_IPI'.
 *               1.Flush local TLB.
 *               2.Report flush TLB process was finished.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    NONE
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
void smp_invalidate_interrupt(void)
{
	int cpu_id = smp_processor_id();
	unsigned long *mmc = &flush_mm->context[cpu_id];

	if (!cpu_isset(cpu_id, flush_cpumask))
		return;

	if (flush_va == FLUSH_ALL) {
		*mmc = NO_CONTEXT;
		if (flush_mm == current->active_mm)
			activate_context(flush_mm);
		else
			cpu_clear(cpu_id, flush_mm->cpu_vm_mask);
	} else {
		unsigned long va = flush_va;

		if (*mmc != NO_CONTEXT) {
			va &= PAGE_MASK;
			va |= (*mmc & MMU_CONTEXT_ASID_MASK);
			__flush_tlb_page(va);
		}
	}
	cpu_clear(cpu_id, flush_cpumask);
}

/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
/* Stop CPU request Routins                                                 */
/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/

/*==========================================================================*
 * Name:         smp_send_stop
 *
 * Description:  This routine requests stop all CPUs.
 *               1.Request other CPU to execute 'stop_this_cpu()'.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    NONE
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
void smp_send_stop(void)
{
	smp_call_function(stop_this_cpu, NULL, 1, 0);
}

/*==========================================================================*
 * Name:         stop_this_cpu
 *
 * Description:  This routine halt CPU.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    NONE
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
static void stop_this_cpu(void *dummy)
{
	int cpu_id = smp_processor_id();

	/*
	 * Remove this CPU:
	 */
	cpu_clear(cpu_id, cpu_online_map);

	/*
	 * PSW IE = 1;
	 * IMASK = 0;
	 * goto SLEEP
	 */
	local_irq_disable();
	outl(0, M32R_ICU_IMASK_PORTL);
	inl(M32R_ICU_IMASK_PORTL);	/* dummy read */
	local_irq_enable();

	for ( ; ; );
}

/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
/* Call function Routins                                                     */
/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/

/*==========================================================================*
 * Name:         smp_call_function
 *
 * Description:  This routine sends a 'CALL_FUNCTION_IPI' to all other CPUs
 *               in the system.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    *func - The function to run. This must be fast and
 *                       non-blocking.
 *               *info - An arbitrary pointer to pass to the function.
 *               nonatomic - currently unused.
 *               wait - If true, wait (atomically) until function has
 *                      completed on other CPUs.
 *
 * Returns:      0 on success, else a negative status code. Does not return
 *               until remote CPUs are nearly ready to execute <<func>> or
 *               are or have executed.
 *
 * Cautions:     You must not call this function with disabled interrupts or
 *               from a hardware interrupt handler, you may call it from a
 *               bottom half handler.
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
int smp_call_function(void (*func) (void *info), void *info, int nonatomic,
	int wait)
{
	struct call_data_struct data;
	int cpus;

#ifdef DEBUG_SMP
	unsigned long flags;
	__save_flags(flags);
	if (!(flags & 0x0040))	/* Interrupt Disable NONONO */
		BUG();
#endif /* DEBUG_SMP */

	/* Holding any lock stops cpus from going down. */
	spin_lock(&call_lock);
	cpus = num_online_cpus() - 1;

	if (!cpus) {
		spin_unlock(&call_lock);
		return 0;
	}

	/* Can deadlock when called with interrupts disabled */
	WARN_ON(irqs_disabled());

	data.func = func;
	data.info = info;
	atomic_set(&data.started, 0);
	data.wait = wait;
	if (wait)
		atomic_set(&data.finished, 0);

	call_data = &data;
	mb();

	/* Send a message to all other CPUs and wait for them to respond */
	send_IPI_allbutself(CALL_FUNCTION_IPI, 0);

	/* Wait for response */
	while (atomic_read(&data.started) != cpus)
		barrier();

	if (wait)
		while (atomic_read(&data.finished) != cpus)
			barrier();
	spin_unlock(&call_lock);

	return 0;
}

/*==========================================================================*
 * Name:         smp_call_function_interrupt
 *
 * Description:  This routine executes on CPU which received
 *               'CALL_FUNCTION_IPI'.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    NONE
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
void smp_call_function_interrupt(void)
{
	void (*func) (void *info) = call_data->func;
	void *info = call_data->info;
	int wait = call_data->wait;

	/*
	 * Notify initiating CPU that I've grabbed the data and am
	 * about to execute the function
	 */
	mb();
	atomic_inc(&call_data->started);
	/*
	 * At this point the info structure may be out of scope unless wait==1
	 */
	irq_enter();
	(*func)(info);
	irq_exit();

	if (wait) {
		mb();
		atomic_inc(&call_data->finished);
	}
}

/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
/* Timer Routins                                                             */
/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/

/*==========================================================================*
 * Name:         smp_send_timer
 *
 * Description:  This routine sends a 'LOCAL_TIMER_IPI' to all other CPUs
 *               in the system.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    NONE
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
void smp_send_timer(void)
{
	send_IPI_allbutself(LOCAL_TIMER_IPI, 1);
}

/*==========================================================================*
 * Name:         smp_send_timer
 *
 * Description:  This routine executes on CPU which received
 *               'LOCAL_TIMER_IPI'.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    *regs - a pointer to the saved regster info
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
void smp_ipi_timer_interrupt(struct pt_regs *regs)
{
	irq_enter();
	smp_local_timer_interrupt(regs);
	irq_exit();
}

/*==========================================================================*
 * Name:         smp_local_timer_interrupt
 *
 * Description:  Local timer interrupt handler. It does both profiling and
 *               process statistics/rescheduling.
 *               We do profiling in every local tick, statistics/rescheduling
 *               happen only every 'profiling multiplier' ticks. The default
 *               multiplier is 1 and it can be changed by writing the new
 *               multiplier value into /proc/profile.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    *regs - a pointer to the saved regster info
 *
 * Returns:      void (cannot fail)
 *
 * Original:     arch/i386/kernel/apic.c
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 * 2003-06-24 hy  use per_cpu structure.
 *==========================================================================*/
void smp_local_timer_interrupt(struct pt_regs *regs)
{
	int user = user_mode(regs);
	int cpu_id = smp_processor_id();

	/*
	 * The profiling function is SMP safe. (nothing can mess
	 * around with "current", and the profiling counters are
	 * updated with atomic operations). This is especially
	 * useful with a profiling multiplier != 1
	 */

	profile_tick(CPU_PROFILING, regs);

	if (--per_cpu(prof_counter, cpu_id) <= 0) {
		/*
		 * The multiplier may have changed since the last time we got
		 * to this point as a result of the user writing to
		 * /proc/profile. In this case we need to adjust the APIC
		 * timer accordingly.
		 *
		 * Interrupts are already masked off at this point.
		 */
		per_cpu(prof_counter, cpu_id)
			= per_cpu(prof_multiplier, cpu_id);
		if (per_cpu(prof_counter, cpu_id)
			!= per_cpu(prof_old_multiplier, cpu_id))
		{
			per_cpu(prof_old_multiplier, cpu_id)
				= per_cpu(prof_counter, cpu_id);
		}

		update_process_times(user);
	}
}

/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
/* Send IPI Routins                                                          */
/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/

/*==========================================================================*
 * Name:         send_IPI_allbutself
 *
 * Description:  This routine sends a IPI to all other CPUs in the system.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    ipi_num - Number of IPI
 *               try -  0 : Send IPI certainly.
 *                     !0 : The following IPI is not sended when Target CPU
 *                          has not received the before IPI.
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
void send_IPI_allbutself(int ipi_num, int try)
{
	cpumask_t cpumask;

	cpumask = cpu_online_map;
	cpu_clear(smp_processor_id(), cpumask);

	send_IPI_mask(cpumask, ipi_num, try);
}

/*==========================================================================*
 * Name:         send_IPI_mask
 *
 * Description:  This routine sends a IPI to CPUs in the system.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    cpu_mask - Bitmap of target CPUs logical ID
 *               ipi_num - Number of IPI
 *               try -  0 : Send IPI certainly.
 *                     !0 : The following IPI is not sended when Target CPU
 *                          has not received the before IPI.
 *
 * Returns:      void (cannot fail)
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
static void send_IPI_mask(cpumask_t cpumask, int ipi_num, int try)
{
	cpumask_t physid_mask, tmp;
	int cpu_id, phys_id;
	int num_cpus = num_online_cpus();

	if (num_cpus <= 1)	/* NO MP */
		return;

	cpus_and(tmp, cpumask, cpu_online_map);
	BUG_ON(!cpus_equal(cpumask, tmp));

	physid_mask = CPU_MASK_NONE;
	for_each_cpu_mask(cpu_id, cpumask){
		if ((phys_id = cpu_to_physid(cpu_id)) != -1)
			cpu_set(phys_id, physid_mask);
	}

	send_IPI_mask_phys(physid_mask, ipi_num, try);
}

/*==========================================================================*
 * Name:         send_IPI_mask_phys
 *
 * Description:  This routine sends a IPI to other CPUs in the system.
 *
 * Born on Date: 2002.02.05
 *
 * Arguments:    cpu_mask - Bitmap of target CPUs physical ID
 *               ipi_num - Number of IPI
 *               try -  0 : Send IPI certainly.
 *                     !0 : The following IPI is not sended when Target CPU
 *                          has not received the before IPI.
 *
 * Returns:      IPICRi regster value.
 *
 * Modification log:
 * Date       Who Description
 * ---------- --- --------------------------------------------------------
 *
 *==========================================================================*/
unsigned long send_IPI_mask_phys(cpumask_t physid_mask, int ipi_num,
	int try)
{
	spinlock_t *ipilock;
	unsigned long flags = 0;
	volatile unsigned long *ipicr_addr;
	unsigned long ipicr_val;
	unsigned long my_physid_mask;
	unsigned long mask = cpus_addr(physid_mask)[0];


	if (mask & ~physids_coerce(phys_cpu_present_map))
		BUG();
	if (ipi_num >= NR_IPIS)
		BUG();

	mask <<= IPI_SHIFT;
	ipilock = &ipi_lock[ipi_num];
	ipicr_addr = (volatile unsigned long *)(M32R_ICU_IPICR_ADDR
		+ (ipi_num << 2));
	my_physid_mask = ~(1 << smp_processor_id());

	/*
	 * lock ipi_lock[i]
	 * check IPICRi == 0
	 * write IPICRi (send IPIi)
	 * unlock ipi_lock[i]
	 */
	__asm__ __volatile__ (
		";; LOCK ipi_lock[i]		\n\t"
		".fillinsn			\n"
		"1:				\n\t"
		"mvfc	%1, psw 		\n\t"
		"clrpsw	#0x40 -> nop		\n\t"
		DCACHE_CLEAR("r4", "r5", "%2")
		"lock	r4, @%2			\n\t"
		"addi	r4, #-1			\n\t"
		"unlock	r4, @%2			\n\t"
		"mvtc	%1, psw			\n\t"
		"bnez	r4, 2f			\n\t"
		LOCK_SECTION_START(".balign 4 \n\t")
		".fillinsn			\n"
		"2:				\n\t"
		"ld	r4, @%2			\n\t"
		"blez	r4, 2b			\n\t"
		"bra	1b			\n\t"
		LOCK_SECTION_END
		";; CHECK IPICRi == 0		\n\t"
		".fillinsn			\n"
		"3:				\n\t"
		"ld	%0, @%3			\n\t"
		"and	%0, %6			\n\t"
		"beqz	%0, 4f			\n\t"
		"bnez	%5, 5f			\n\t"
		"bra	3b			\n\t"
		";; WRITE IPICRi (send IPIi)	\n\t"
		".fillinsn			\n"
		"4:				\n\t"
		"st	%4, @%3			\n\t"
		";; UNLOCK ipi_lock[i]		\n\t"
		".fillinsn			\n"
		"5:				\n\t"
		"ldi	r4, #1			\n\t"
		"st	r4, @%2			\n\t"
		: "=&r"(ipicr_val)
		: "r"(flags), "r"(&ipilock->slock), "r"(ipicr_addr),
		  "r"(mask), "r"(try), "r"(my_physid_mask)
		: "memory", "r4"
#ifdef CONFIG_CHIP_M32700_TS1
		, "r5"
#endif	/* CONFIG_CHIP_M32700_TS1 */
	);

	return ipicr_val;
}