smp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * arch/sh/kernel/smp.c
  3. *
  4. * SMP support for the SuperH processors.
  5. *
  6. * Copyright (C) 2002, 2003 Paul Mundt
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/cache.h>
  14. #include <linux/cpumask.h>
  15. #include <linux/delay.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/threads.h>
  20. #include <linux/module.h>
  21. #include <linux/time.h>
  22. #include <linux/timex.h>
  23. #include <linux/sched.h>
  24. #include <linux/module.h>
  25. #include <asm/atomic.h>
  26. #include <asm/processor.h>
  27. #include <asm/system.h>
  28. #include <asm/mmu_context.h>
  29. #include <asm/smp.h>
  30. /*
  31. * This was written with the Sega Saturn (SMP SH-2 7604) in mind,
  32. * but is designed to be usable regardless if there's an MMU
  33. * present or not.
  34. */
  35. struct sh_cpuinfo cpu_data[NR_CPUS];
  36. extern void per_cpu_trap_init(void);
  37. cpumask_t cpu_possible_map;
  38. EXPORT_SYMBOL(cpu_possible_map);
  39. cpumask_t cpu_online_map;
  40. EXPORT_SYMBOL(cpu_online_map);
  41. static atomic_t cpus_booted = ATOMIC_INIT(0);
  42. /* These are defined by the board-specific code. */
  43. /*
  44. * Cause the function described by call_data to be executed on the passed
  45. * cpu. When the function has finished, increment the finished field of
  46. * call_data.
  47. */
  48. void __smp_send_ipi(unsigned int cpu, unsigned int action);
  49. /*
  50. * Find the number of available processors
  51. */
  52. unsigned int __smp_probe_cpus(void);
  53. /*
  54. * Start a particular processor
  55. */
  56. void __smp_slave_init(unsigned int cpu);
  57. /*
  58. * Run specified function on a particular processor.
  59. */
  60. void __smp_call_function(unsigned int cpu);
  61. static inline void __init smp_store_cpu_info(unsigned int cpu)
  62. {
  63. cpu_data[cpu].loops_per_jiffy = loops_per_jiffy;
  64. }
  65. void __init smp_prepare_cpus(unsigned int max_cpus)
  66. {
  67. unsigned int cpu = smp_processor_id();
  68. int i;
  69. atomic_set(&cpus_booted, 1);
  70. smp_store_cpu_info(cpu);
  71. for (i = 0; i < __smp_probe_cpus(); i++)
  72. cpu_set(i, cpu_possible_map);
  73. }
  74. void __devinit smp_prepare_boot_cpu(void)
  75. {
  76. unsigned int cpu = smp_processor_id();
  77. cpu_set(cpu, cpu_online_map);
  78. cpu_set(cpu, cpu_possible_map);
  79. }
  80. int __cpu_up(unsigned int cpu)
  81. {
  82. struct task_struct *tsk;
  83. tsk = fork_idle(cpu);
  84. if (IS_ERR(tsk))
  85. panic("Failed forking idle task for cpu %d\n", cpu);
  86. task_thread_info(tsk)->cpu = cpu;
  87. cpu_set(cpu, cpu_online_map);
  88. return 0;
  89. }
  90. int start_secondary(void *unused)
  91. {
  92. unsigned int cpu;
  93. cpu = smp_processor_id();
  94. atomic_inc(&init_mm.mm_count);
  95. current->active_mm = &init_mm;
  96. smp_store_cpu_info(cpu);
  97. __smp_slave_init(cpu);
  98. preempt_disable();
  99. per_cpu_trap_init();
  100. atomic_inc(&cpus_booted);
  101. cpu_idle();
  102. return 0;
  103. }
  104. void __init smp_cpus_done(unsigned int max_cpus)
  105. {
  106. smp_mb();
  107. }
  108. void smp_send_reschedule(int cpu)
  109. {
  110. __smp_send_ipi(cpu, SMP_MSG_RESCHEDULE);
  111. }
  112. static void stop_this_cpu(void *unused)
  113. {
  114. cpu_clear(smp_processor_id(), cpu_online_map);
  115. local_irq_disable();
  116. for (;;)
  117. cpu_relax();
  118. }
  119. void smp_send_stop(void)
  120. {
  121. smp_call_function(stop_this_cpu, 0, 1, 0);
  122. }
  123. struct smp_fn_call_struct smp_fn_call = {
  124. .lock = SPIN_LOCK_UNLOCKED,
  125. .finished = ATOMIC_INIT(0),
  126. };
  127. /*
  128. * The caller of this wants the passed function to run on every cpu. If wait
  129. * is set, wait until all cpus have finished the function before returning.
  130. * The lock is here to protect the call structure.
  131. * You must not call this function with disabled interrupts or from a
  132. * hardware interrupt handler or from a bottom half handler.
  133. */
  134. int smp_call_function(void (*func)(void *info), void *info, int retry, int wait)
  135. {
  136. unsigned int nr_cpus = atomic_read(&cpus_booted);
  137. int i;
  138. if (nr_cpus < 2)
  139. return 0;
  140. /* Can deadlock when called with interrupts disabled */
  141. WARN_ON(irqs_disabled());
  142. spin_lock(&smp_fn_call.lock);
  143. atomic_set(&smp_fn_call.finished, 0);
  144. smp_fn_call.fn = func;
  145. smp_fn_call.data = info;
  146. for (i = 0; i < nr_cpus; i++)
  147. if (i != smp_processor_id())
  148. __smp_call_function(i);
  149. if (wait)
  150. while (atomic_read(&smp_fn_call.finished) != (nr_cpus - 1));
  151. spin_unlock(&smp_fn_call.lock);
  152. return 0;
  153. }
  154. /* Not really SMP stuff ... */
  155. int setup_profiling_timer(unsigned int multiplier)
  156. {
  157. return 0;
  158. }