smp-mt.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * This program is free software; you can distribute it and/or modify it
  3. * under the terms of the GNU General Public License (Version 2) as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  8. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  9. * for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License along
  12. * with this program; if not, write to the Free Software Foundation, Inc.,
  13. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  14. *
  15. * Copyright (C) 2004, 05, 06 MIPS Technologies, Inc.
  16. * Elizabeth Clarke (beth@mips.com)
  17. * Ralf Baechle (ralf@linux-mips.org)
  18. * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org)
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/sched.h>
  22. #include <linux/cpumask.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/compiler.h>
  25. #include <asm/atomic.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/cpu.h>
  28. #include <asm/processor.h>
  29. #include <asm/system.h>
  30. #include <asm/hardirq.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/smp.h>
  33. #include <asm/time.h>
  34. #include <asm/mipsregs.h>
  35. #include <asm/mipsmtregs.h>
  36. #include <asm/mips_mt.h>
  37. #include <asm/mips-boards/maltaint.h> /* This is f*cking wrong */
  38. #define MIPS_CPU_IPI_RESCHED_IRQ 0
  39. #define MIPS_CPU_IPI_CALL_IRQ 1
  40. static int cpu_ipi_resched_irq, cpu_ipi_call_irq;
  41. #if 0
  42. static void dump_mtregisters(int vpe, int tc)
  43. {
  44. printk("vpe %d tc %d\n", vpe, tc);
  45. settc(tc);
  46. printk(" c0 status 0x%lx\n", read_vpe_c0_status());
  47. printk(" vpecontrol 0x%lx\n", read_vpe_c0_vpecontrol());
  48. printk(" vpeconf0 0x%lx\n", read_vpe_c0_vpeconf0());
  49. printk(" tcstatus 0x%lx\n", read_tc_c0_tcstatus());
  50. printk(" tcrestart 0x%lx\n", read_tc_c0_tcrestart());
  51. printk(" tcbind 0x%lx\n", read_tc_c0_tcbind());
  52. printk(" tchalt 0x%lx\n", read_tc_c0_tchalt());
  53. }
  54. #endif
  55. void __init sanitize_tlb_entries(void)
  56. {
  57. int i, tlbsiz;
  58. unsigned long mvpconf0, ncpu;
  59. if (!cpu_has_mipsmt)
  60. return;
  61. /* Enable VPC */
  62. set_c0_mvpcontrol(MVPCONTROL_VPC);
  63. back_to_back_c0_hazard();
  64. /* Disable TLB sharing */
  65. clear_c0_mvpcontrol(MVPCONTROL_STLB);
  66. mvpconf0 = read_c0_mvpconf0();
  67. printk(KERN_INFO "MVPConf0 0x%lx TLBS %lx PTLBE %ld\n", mvpconf0,
  68. (mvpconf0 & MVPCONF0_TLBS) >> MVPCONF0_TLBS_SHIFT,
  69. (mvpconf0 & MVPCONF0_PTLBE) >> MVPCONF0_PTLBE_SHIFT);
  70. tlbsiz = (mvpconf0 & MVPCONF0_PTLBE) >> MVPCONF0_PTLBE_SHIFT;
  71. ncpu = ((mvpconf0 & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1;
  72. printk(" tlbsiz %d ncpu %ld\n", tlbsiz, ncpu);
  73. if (tlbsiz > 0) {
  74. /* share them out across the vpe's */
  75. tlbsiz /= ncpu;
  76. printk(KERN_INFO "setting Config1.MMU_size to %d\n", tlbsiz);
  77. for (i = 0; i < ncpu; i++) {
  78. settc(i);
  79. if (i == 0)
  80. write_c0_config1((read_c0_config1() & ~(0x3f << 25)) | (tlbsiz << 25));
  81. else
  82. write_vpe_c0_config1((read_vpe_c0_config1() & ~(0x3f << 25)) |
  83. (tlbsiz << 25));
  84. }
  85. }
  86. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  87. }
  88. static void ipi_resched_dispatch (struct pt_regs *regs)
  89. {
  90. do_IRQ(MIPSCPU_INT_BASE + MIPS_CPU_IPI_RESCHED_IRQ, regs);
  91. }
  92. static void ipi_call_dispatch (struct pt_regs *regs)
  93. {
  94. do_IRQ(MIPSCPU_INT_BASE + MIPS_CPU_IPI_CALL_IRQ, regs);
  95. }
  96. irqreturn_t ipi_resched_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  97. {
  98. return IRQ_HANDLED;
  99. }
  100. irqreturn_t ipi_call_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  101. {
  102. smp_call_function_interrupt();
  103. return IRQ_HANDLED;
  104. }
  105. static struct irqaction irq_resched = {
  106. .handler = ipi_resched_interrupt,
  107. .flags = SA_INTERRUPT,
  108. .name = "IPI_resched"
  109. };
  110. static struct irqaction irq_call = {
  111. .handler = ipi_call_interrupt,
  112. .flags = SA_INTERRUPT,
  113. .name = "IPI_call"
  114. };
  115. /*
  116. * Common setup before any secondaries are started
  117. * Make sure all CPU's are in a sensible state before we boot any of the
  118. * secondarys
  119. */
  120. void plat_smp_setup(void)
  121. {
  122. unsigned long val;
  123. int i, num;
  124. #ifdef CONFIG_MIPS_MT_FPAFF
  125. /* If we have an FPU, enroll ourselves in the FPU-full mask */
  126. if (cpu_has_fpu)
  127. cpu_set(0, mt_fpu_cpumask);
  128. #endif /* CONFIG_MIPS_MT_FPAFF */
  129. if (!cpu_has_mipsmt)
  130. return;
  131. /* disable MT so we can configure */
  132. dvpe();
  133. dmt();
  134. mips_mt_set_cpuoptions();
  135. /* Put MVPE's into 'configuration state' */
  136. set_c0_mvpcontrol(MVPCONTROL_VPC);
  137. val = read_c0_mvpconf0();
  138. /* we'll always have more TC's than VPE's, so loop setting everything
  139. to a sensible state */
  140. for (i = 0, num = 0; i <= ((val & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT); i++) {
  141. settc(i);
  142. /* VPE's */
  143. if (i <= ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT)) {
  144. /* deactivate all but vpe0 */
  145. if (i != 0) {
  146. unsigned long tmp = read_vpe_c0_vpeconf0();
  147. tmp &= ~VPECONF0_VPA;
  148. /* master VPE */
  149. tmp |= VPECONF0_MVP;
  150. write_vpe_c0_vpeconf0(tmp);
  151. /* Record this as available CPU */
  152. cpu_set(i, phys_cpu_present_map);
  153. __cpu_number_map[i] = ++num;
  154. __cpu_logical_map[num] = i;
  155. }
  156. /* disable multi-threading with TC's */
  157. write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() & ~VPECONTROL_TE);
  158. if (i != 0) {
  159. write_vpe_c0_status((read_c0_status() & ~(ST0_IM | ST0_IE | ST0_KSU)) | ST0_CU0);
  160. /* set config to be the same as vpe0, particularly kseg0 coherency alg */
  161. write_vpe_c0_config( read_c0_config());
  162. /* make sure there are no software interrupts pending */
  163. write_vpe_c0_cause(read_vpe_c0_cause() & ~(C_SW1|C_SW0));
  164. /* Propagate Config7 */
  165. write_vpe_c0_config7(read_c0_config7());
  166. }
  167. }
  168. /* TC's */
  169. if (i != 0) {
  170. unsigned long tmp;
  171. /* bind a TC to each VPE, May as well put all excess TC's
  172. on the last VPE */
  173. if ( i >= (((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT)+1) )
  174. write_tc_c0_tcbind(read_tc_c0_tcbind() | ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) );
  175. else {
  176. write_tc_c0_tcbind( read_tc_c0_tcbind() | i);
  177. /* and set XTC */
  178. write_vpe_c0_vpeconf0( read_vpe_c0_vpeconf0() | (i << VPECONF0_XTC_SHIFT));
  179. }
  180. tmp = read_tc_c0_tcstatus();
  181. /* mark not allocated and not dynamically allocatable */
  182. tmp &= ~(TCSTATUS_A | TCSTATUS_DA);
  183. tmp |= TCSTATUS_IXMT; /* interrupt exempt */
  184. write_tc_c0_tcstatus(tmp);
  185. write_tc_c0_tchalt(TCHALT_H);
  186. }
  187. }
  188. /* Release config state */
  189. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  190. /* We'll wait until starting the secondaries before starting MVPE */
  191. printk(KERN_INFO "Detected %i available secondary CPU(s)\n", num);
  192. }
  193. void __init plat_prepare_cpus(unsigned int max_cpus)
  194. {
  195. /* set up ipi interrupts */
  196. if (cpu_has_vint) {
  197. set_vi_handler (MIPS_CPU_IPI_RESCHED_IRQ, ipi_resched_dispatch);
  198. set_vi_handler (MIPS_CPU_IPI_CALL_IRQ, ipi_call_dispatch);
  199. }
  200. cpu_ipi_resched_irq = MIPSCPU_INT_BASE + MIPS_CPU_IPI_RESCHED_IRQ;
  201. cpu_ipi_call_irq = MIPSCPU_INT_BASE + MIPS_CPU_IPI_CALL_IRQ;
  202. setup_irq(cpu_ipi_resched_irq, &irq_resched);
  203. setup_irq(cpu_ipi_call_irq, &irq_call);
  204. /* need to mark IPI's as IRQ_PER_CPU */
  205. irq_desc[cpu_ipi_resched_irq].status |= IRQ_PER_CPU;
  206. irq_desc[cpu_ipi_call_irq].status |= IRQ_PER_CPU;
  207. }
  208. /*
  209. * Setup the PC, SP, and GP of a secondary processor and start it
  210. * running!
  211. * smp_bootstrap is the place to resume from
  212. * __KSTK_TOS(idle) is apparently the stack pointer
  213. * (unsigned long)idle->thread_info the gp
  214. * assumes a 1:1 mapping of TC => VPE
  215. */
  216. void prom_boot_secondary(int cpu, struct task_struct *idle)
  217. {
  218. struct thread_info *gp = task_thread_info(idle);
  219. dvpe();
  220. set_c0_mvpcontrol(MVPCONTROL_VPC);
  221. settc(cpu);
  222. /* restart */
  223. write_tc_c0_tcrestart((unsigned long)&smp_bootstrap);
  224. /* enable the tc this vpe/cpu will be running */
  225. write_tc_c0_tcstatus((read_tc_c0_tcstatus() & ~TCSTATUS_IXMT) | TCSTATUS_A);
  226. write_tc_c0_tchalt(0);
  227. /* enable the VPE */
  228. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA);
  229. /* stack pointer */
  230. write_tc_gpr_sp( __KSTK_TOS(idle));
  231. /* global pointer */
  232. write_tc_gpr_gp((unsigned long)gp);
  233. flush_icache_range((unsigned long)gp,
  234. (unsigned long)(gp + sizeof(struct thread_info)));
  235. /* finally out of configuration and into chaos */
  236. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  237. evpe(EVPE_ENABLE);
  238. }
  239. void prom_init_secondary(void)
  240. {
  241. write_c0_status((read_c0_status() & ~ST0_IM ) |
  242. (STATUSF_IP0 | STATUSF_IP1 | STATUSF_IP7));
  243. }
  244. void prom_smp_finish(void)
  245. {
  246. write_c0_compare(read_c0_count() + (8* mips_hpt_frequency/HZ));
  247. #ifdef CONFIG_MIPS_MT_FPAFF
  248. /* If we have an FPU, enroll ourselves in the FPU-full mask */
  249. if (cpu_has_fpu)
  250. cpu_set(smp_processor_id(), mt_fpu_cpumask);
  251. #endif /* CONFIG_MIPS_MT_FPAFF */
  252. local_irq_enable();
  253. }
  254. void prom_cpus_done(void)
  255. {
  256. }
  257. void core_send_ipi(int cpu, unsigned int action)
  258. {
  259. int i;
  260. unsigned long flags;
  261. int vpflags;
  262. local_irq_save (flags);
  263. vpflags = dvpe(); /* cant access the other CPU's registers whilst MVPE enabled */
  264. switch (action) {
  265. case SMP_CALL_FUNCTION:
  266. i = C_SW1;
  267. break;
  268. case SMP_RESCHEDULE_YOURSELF:
  269. default:
  270. i = C_SW0;
  271. break;
  272. }
  273. /* 1:1 mapping of vpe and tc... */
  274. settc(cpu);
  275. write_vpe_c0_cause(read_vpe_c0_cause() | i);
  276. evpe(vpflags);
  277. local_irq_restore(flags);
  278. }