smp.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* smp.c: Sparc SMP support.
  2. *
  3. * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
  4. * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  5. * Copyright (C) 2004 Keith M Wesolowski (wesolows@foobazco.org)
  6. */
  7. #include <asm/head.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/threads.h>
  11. #include <linux/smp.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel_stat.h>
  15. #include <linux/init.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/mm.h>
  18. #include <linux/fs.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/cache.h>
  21. #include <linux/delay.h>
  22. #include <asm/ptrace.h>
  23. #include <asm/atomic.h>
  24. #include <asm/irq.h>
  25. #include <asm/page.h>
  26. #include <asm/pgalloc.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/oplib.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/tlbflush.h>
  31. #include <asm/cpudata.h>
  32. volatile int smp_processors_ready = 0;
  33. int smp_num_cpus = 1;
  34. volatile unsigned long cpu_callin_map[NR_CPUS] __initdata = {0,};
  35. unsigned char boot_cpu_id = 0;
  36. unsigned char boot_cpu_id4 = 0; /* boot_cpu_id << 2 */
  37. int smp_activated = 0;
  38. volatile int __cpu_number_map[NR_CPUS];
  39. volatile int __cpu_logical_map[NR_CPUS];
  40. cpumask_t cpu_online_map = CPU_MASK_NONE;
  41. cpumask_t phys_cpu_present_map = CPU_MASK_NONE;
  42. cpumask_t smp_commenced_mask = CPU_MASK_NONE;
  43. /* The only guaranteed locking primitive available on all Sparc
  44. * processors is 'ldstub [%reg + immediate], %dest_reg' which atomically
  45. * places the current byte at the effective address into dest_reg and
  46. * places 0xff there afterwards. Pretty lame locking primitive
  47. * compared to the Alpha and the Intel no? Most Sparcs have 'swap'
  48. * instruction which is much better...
  49. */
  50. /* Used to make bitops atomic */
  51. unsigned char bitops_spinlock = 0;
  52. void __init smp_store_cpu_info(int id)
  53. {
  54. int cpu_node;
  55. cpu_data(id).udelay_val = loops_per_jiffy;
  56. cpu_find_by_mid(id, &cpu_node);
  57. cpu_data(id).clock_tick = prom_getintdefault(cpu_node,
  58. "clock-frequency", 0);
  59. cpu_data(id).prom_node = cpu_node;
  60. cpu_data(id).mid = cpu_get_hwmid(cpu_node);
  61. if (cpu_data(id).mid < 0)
  62. panic("No MID found for CPU%d at node 0x%08d", id, cpu_node);
  63. }
  64. void __init smp_cpus_done(unsigned int max_cpus)
  65. {
  66. extern void smp4m_smp_done(void);
  67. unsigned long bogosum = 0;
  68. int cpu, num;
  69. for (cpu = 0, num = 0; cpu < NR_CPUS; cpu++)
  70. if (cpu_online(cpu)) {
  71. num++;
  72. bogosum += cpu_data(cpu).udelay_val;
  73. }
  74. printk("Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
  75. num, bogosum/(500000/HZ),
  76. (bogosum/(5000/HZ))%100);
  77. BUG_ON(sparc_cpu_model != sun4m);
  78. smp4m_smp_done();
  79. }
  80. void cpu_panic(void)
  81. {
  82. printk("CPU[%d]: Returns from cpu_idle!\n", smp_processor_id());
  83. panic("SMP bolixed\n");
  84. }
  85. struct linux_prom_registers smp_penguin_ctable __initdata = { 0 };
  86. void smp_send_reschedule(int cpu)
  87. {
  88. /* See sparc64 */
  89. }
  90. void smp_send_stop(void)
  91. {
  92. }
  93. void smp_flush_cache_all(void)
  94. {
  95. xc0((smpfunc_t) BTFIXUP_CALL(local_flush_cache_all));
  96. local_flush_cache_all();
  97. }
  98. void smp_flush_tlb_all(void)
  99. {
  100. xc0((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_all));
  101. local_flush_tlb_all();
  102. }
  103. void smp_flush_cache_mm(struct mm_struct *mm)
  104. {
  105. if(mm->context != NO_CONTEXT) {
  106. cpumask_t cpu_mask = mm->cpu_vm_mask;
  107. cpu_clear(smp_processor_id(), cpu_mask);
  108. if (!cpus_empty(cpu_mask))
  109. xc1((smpfunc_t) BTFIXUP_CALL(local_flush_cache_mm), (unsigned long) mm);
  110. local_flush_cache_mm(mm);
  111. }
  112. }
  113. void smp_flush_tlb_mm(struct mm_struct *mm)
  114. {
  115. if(mm->context != NO_CONTEXT) {
  116. cpumask_t cpu_mask = mm->cpu_vm_mask;
  117. cpu_clear(smp_processor_id(), cpu_mask);
  118. if (!cpus_empty(cpu_mask)) {
  119. xc1((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_mm), (unsigned long) mm);
  120. if(atomic_read(&mm->mm_users) == 1 && current->active_mm == mm)
  121. mm->cpu_vm_mask = cpumask_of_cpu(smp_processor_id());
  122. }
  123. local_flush_tlb_mm(mm);
  124. }
  125. }
  126. void smp_flush_cache_range(struct vm_area_struct *vma, unsigned long start,
  127. unsigned long end)
  128. {
  129. struct mm_struct *mm = vma->vm_mm;
  130. if (mm->context != NO_CONTEXT) {
  131. cpumask_t cpu_mask = mm->cpu_vm_mask;
  132. cpu_clear(smp_processor_id(), cpu_mask);
  133. if (!cpus_empty(cpu_mask))
  134. xc3((smpfunc_t) BTFIXUP_CALL(local_flush_cache_range), (unsigned long) vma, start, end);
  135. local_flush_cache_range(vma, start, end);
  136. }
  137. }
  138. void smp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  139. unsigned long end)
  140. {
  141. struct mm_struct *mm = vma->vm_mm;
  142. if (mm->context != NO_CONTEXT) {
  143. cpumask_t cpu_mask = mm->cpu_vm_mask;
  144. cpu_clear(smp_processor_id(), cpu_mask);
  145. if (!cpus_empty(cpu_mask))
  146. xc3((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_range), (unsigned long) vma, start, end);
  147. local_flush_tlb_range(vma, start, end);
  148. }
  149. }
  150. void smp_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
  151. {
  152. struct mm_struct *mm = vma->vm_mm;
  153. if(mm->context != NO_CONTEXT) {
  154. cpumask_t cpu_mask = mm->cpu_vm_mask;
  155. cpu_clear(smp_processor_id(), cpu_mask);
  156. if (!cpus_empty(cpu_mask))
  157. xc2((smpfunc_t) BTFIXUP_CALL(local_flush_cache_page), (unsigned long) vma, page);
  158. local_flush_cache_page(vma, page);
  159. }
  160. }
  161. void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  162. {
  163. struct mm_struct *mm = vma->vm_mm;
  164. if(mm->context != NO_CONTEXT) {
  165. cpumask_t cpu_mask = mm->cpu_vm_mask;
  166. cpu_clear(smp_processor_id(), cpu_mask);
  167. if (!cpus_empty(cpu_mask))
  168. xc2((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_page), (unsigned long) vma, page);
  169. local_flush_tlb_page(vma, page);
  170. }
  171. }
  172. void smp_reschedule_irq(void)
  173. {
  174. set_need_resched();
  175. }
  176. void smp_flush_page_to_ram(unsigned long page)
  177. {
  178. /* Current theory is that those who call this are the one's
  179. * who have just dirtied their cache with the pages contents
  180. * in kernel space, therefore we only run this on local cpu.
  181. *
  182. * XXX This experiment failed, research further... -DaveM
  183. */
  184. #if 1
  185. xc1((smpfunc_t) BTFIXUP_CALL(local_flush_page_to_ram), page);
  186. #endif
  187. local_flush_page_to_ram(page);
  188. }
  189. void smp_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
  190. {
  191. cpumask_t cpu_mask = mm->cpu_vm_mask;
  192. cpu_clear(smp_processor_id(), cpu_mask);
  193. if (!cpus_empty(cpu_mask))
  194. xc2((smpfunc_t) BTFIXUP_CALL(local_flush_sig_insns), (unsigned long) mm, insn_addr);
  195. local_flush_sig_insns(mm, insn_addr);
  196. }
  197. extern unsigned int lvl14_resolution;
  198. /* /proc/profile writes can call this, don't __init it please. */
  199. static DEFINE_SPINLOCK(prof_setup_lock);
  200. int setup_profiling_timer(unsigned int multiplier)
  201. {
  202. int i;
  203. unsigned long flags;
  204. /* Prevent level14 ticker IRQ flooding. */
  205. if((!multiplier) || (lvl14_resolution / multiplier) < 500)
  206. return -EINVAL;
  207. spin_lock_irqsave(&prof_setup_lock, flags);
  208. for_each_possible_cpu(i) {
  209. load_profile_irq(i, lvl14_resolution / multiplier);
  210. prof_multiplier(i) = multiplier;
  211. }
  212. spin_unlock_irqrestore(&prof_setup_lock, flags);
  213. return 0;
  214. }
  215. void __init smp_prepare_cpus(unsigned int max_cpus)
  216. {
  217. extern void smp4m_boot_cpus(void);
  218. int i, cpuid, ncpus, extra;
  219. BUG_ON(sparc_cpu_model != sun4m);
  220. printk("Entering SMP Mode...\n");
  221. ncpus = 1;
  222. extra = 0;
  223. for (i = 0; !cpu_find_by_instance(i, NULL, &cpuid); i++) {
  224. if (cpuid == boot_cpu_id)
  225. continue;
  226. if (cpuid < NR_CPUS && ncpus++ < max_cpus)
  227. cpu_set(cpuid, phys_cpu_present_map);
  228. else
  229. extra++;
  230. }
  231. if (max_cpus >= NR_CPUS && extra)
  232. printk("Warning: NR_CPUS is too low to start all cpus\n");
  233. smp_store_cpu_info(boot_cpu_id);
  234. smp4m_boot_cpus();
  235. }
  236. void __devinit smp_prepare_boot_cpu(void)
  237. {
  238. int cpuid = hard_smp_processor_id();
  239. if (cpuid >= NR_CPUS) {
  240. prom_printf("Serious problem, boot cpu id >= NR_CPUS\n");
  241. prom_halt();
  242. }
  243. if (cpuid != 0)
  244. printk("boot cpu id != 0, this could work but is untested\n");
  245. current_thread_info()->cpu = cpuid;
  246. cpu_set(cpuid, cpu_online_map);
  247. cpu_set(cpuid, phys_cpu_present_map);
  248. }
  249. int __devinit __cpu_up(unsigned int cpu)
  250. {
  251. extern int smp4m_boot_one_cpu(int);
  252. int ret;
  253. ret = smp4m_boot_one_cpu(cpu);
  254. if (!ret) {
  255. cpu_set(cpu, smp_commenced_mask);
  256. while (!cpu_online(cpu))
  257. mb();
  258. }
  259. return ret;
  260. }
  261. void smp_bogo(struct seq_file *m)
  262. {
  263. int i;
  264. for_each_online_cpu(i) {
  265. seq_printf(m,
  266. "Cpu%dBogo\t: %lu.%02lu\n",
  267. i,
  268. cpu_data(i).udelay_val/(500000/HZ),
  269. (cpu_data(i).udelay_val/(5000/HZ))%100);
  270. }
  271. }
  272. void smp_info(struct seq_file *m)
  273. {
  274. int i;
  275. seq_printf(m, "State:\n");
  276. for_each_online_cpu(i)
  277. seq_printf(m, "CPU%d\t\t: online\n", i);
  278. }