smp-mt.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. if (!cpu_has_mipsmt)
  125. return;
  126. /* disable MT so we can configure */
  127. dvpe();
  128. dmt();
  129. mips_mt_set_cpuoptions();
  130. /* Put MVPE's into 'configuration state' */
  131. set_c0_mvpcontrol(MVPCONTROL_VPC);
  132. val = read_c0_mvpconf0();
  133. /* we'll always have more TC's than VPE's, so loop setting everything
  134. to a sensible state */
  135. for (i = 0, num = 0; i <= ((val & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT); i++) {
  136. settc(i);
  137. /* VPE's */
  138. if (i <= ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT)) {
  139. /* deactivate all but vpe0 */
  140. if (i != 0) {
  141. unsigned long tmp = read_vpe_c0_vpeconf0();
  142. tmp &= ~VPECONF0_VPA;
  143. /* master VPE */
  144. tmp |= VPECONF0_MVP;
  145. write_vpe_c0_vpeconf0(tmp);
  146. /* Record this as available CPU */
  147. cpu_set(i, phys_cpu_present_map);
  148. __cpu_number_map[i] = ++num;
  149. __cpu_logical_map[num] = i;
  150. }
  151. /* disable multi-threading with TC's */
  152. write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() & ~VPECONTROL_TE);
  153. if (i != 0) {
  154. write_vpe_c0_status((read_c0_status() & ~(ST0_IM | ST0_IE | ST0_KSU)) | ST0_CU0);
  155. /* set config to be the same as vpe0, particularly kseg0 coherency alg */
  156. write_vpe_c0_config( read_c0_config());
  157. /* make sure there are no software interrupts pending */
  158. write_vpe_c0_cause(read_vpe_c0_cause() & ~(C_SW1|C_SW0));
  159. /* Propagate Config7 */
  160. write_vpe_c0_config7(read_c0_config7());
  161. }
  162. }
  163. /* TC's */
  164. if (i != 0) {
  165. unsigned long tmp;
  166. /* bind a TC to each VPE, May as well put all excess TC's
  167. on the last VPE */
  168. if ( i >= (((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT)+1) )
  169. write_tc_c0_tcbind(read_tc_c0_tcbind() | ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) );
  170. else {
  171. write_tc_c0_tcbind( read_tc_c0_tcbind() | i);
  172. /* and set XTC */
  173. write_vpe_c0_vpeconf0( read_vpe_c0_vpeconf0() | (i << VPECONF0_XTC_SHIFT));
  174. }
  175. tmp = read_tc_c0_tcstatus();
  176. /* mark not allocated and not dynamically allocatable */
  177. tmp &= ~(TCSTATUS_A | TCSTATUS_DA);
  178. tmp |= TCSTATUS_IXMT; /* interrupt exempt */
  179. write_tc_c0_tcstatus(tmp);
  180. write_tc_c0_tchalt(TCHALT_H);
  181. }
  182. }
  183. /* Release config state */
  184. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  185. /* We'll wait until starting the secondaries before starting MVPE */
  186. printk(KERN_INFO "Detected %i available secondary CPU(s)\n", num);
  187. }
  188. void __init plat_prepare_cpus(unsigned int max_cpus)
  189. {
  190. /* set up ipi interrupts */
  191. if (cpu_has_vint) {
  192. set_vi_handler (MIPS_CPU_IPI_RESCHED_IRQ, ipi_resched_dispatch);
  193. set_vi_handler (MIPS_CPU_IPI_CALL_IRQ, ipi_call_dispatch);
  194. }
  195. cpu_ipi_resched_irq = MIPSCPU_INT_BASE + MIPS_CPU_IPI_RESCHED_IRQ;
  196. cpu_ipi_call_irq = MIPSCPU_INT_BASE + MIPS_CPU_IPI_CALL_IRQ;
  197. setup_irq(cpu_ipi_resched_irq, &irq_resched);
  198. setup_irq(cpu_ipi_call_irq, &irq_call);
  199. /* need to mark IPI's as IRQ_PER_CPU */
  200. irq_desc[cpu_ipi_resched_irq].status |= IRQ_PER_CPU;
  201. irq_desc[cpu_ipi_call_irq].status |= IRQ_PER_CPU;
  202. }
  203. /*
  204. * Setup the PC, SP, and GP of a secondary processor and start it
  205. * running!
  206. * smp_bootstrap is the place to resume from
  207. * __KSTK_TOS(idle) is apparently the stack pointer
  208. * (unsigned long)idle->thread_info the gp
  209. * assumes a 1:1 mapping of TC => VPE
  210. */
  211. void prom_boot_secondary(int cpu, struct task_struct *idle)
  212. {
  213. struct thread_info *gp = task_thread_info(idle);
  214. dvpe();
  215. set_c0_mvpcontrol(MVPCONTROL_VPC);
  216. settc(cpu);
  217. /* restart */
  218. write_tc_c0_tcrestart((unsigned long)&smp_bootstrap);
  219. /* enable the tc this vpe/cpu will be running */
  220. write_tc_c0_tcstatus((read_tc_c0_tcstatus() & ~TCSTATUS_IXMT) | TCSTATUS_A);
  221. write_tc_c0_tchalt(0);
  222. /* enable the VPE */
  223. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA);
  224. /* stack pointer */
  225. write_tc_gpr_sp( __KSTK_TOS(idle));
  226. /* global pointer */
  227. write_tc_gpr_gp((unsigned long)gp);
  228. flush_icache_range((unsigned long)gp,
  229. (unsigned long)(gp + sizeof(struct thread_info)));
  230. /* finally out of configuration and into chaos */
  231. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  232. evpe(EVPE_ENABLE);
  233. }
  234. void prom_init_secondary(void)
  235. {
  236. write_c0_status((read_c0_status() & ~ST0_IM ) |
  237. (STATUSF_IP0 | STATUSF_IP1 | STATUSF_IP7));
  238. }
  239. void prom_smp_finish(void)
  240. {
  241. write_c0_compare(read_c0_count() + (8* mips_hpt_frequency/HZ));
  242. local_irq_enable();
  243. }
  244. void prom_cpus_done(void)
  245. {
  246. }
  247. void core_send_ipi(int cpu, unsigned int action)
  248. {
  249. int i;
  250. unsigned long flags;
  251. int vpflags;
  252. local_irq_save (flags);
  253. vpflags = dvpe(); /* cant access the other CPU's registers whilst MVPE enabled */
  254. switch (action) {
  255. case SMP_CALL_FUNCTION:
  256. i = C_SW1;
  257. break;
  258. case SMP_RESCHEDULE_YOURSELF:
  259. default:
  260. i = C_SW0;
  261. break;
  262. }
  263. /* 1:1 mapping of vpe and tc... */
  264. settc(cpu);
  265. write_vpe_c0_cause(read_vpe_c0_cause() | i);
  266. evpe(vpflags);
  267. local_irq_restore(flags);
  268. }