smp_mt.c 8.8 KB

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