smtc.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. /* Copyright (C) 2004 Mips Technologies, Inc */
  2. #include <linux/clockchips.h>
  3. #include <linux/kernel.h>
  4. #include <linux/sched.h>
  5. #include <linux/cpumask.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/kernel_stat.h>
  8. #include <linux/module.h>
  9. #include <asm/cpu.h>
  10. #include <asm/processor.h>
  11. #include <asm/atomic.h>
  12. #include <asm/system.h>
  13. #include <asm/hardirq.h>
  14. #include <asm/hazards.h>
  15. #include <asm/irq.h>
  16. #include <asm/mmu_context.h>
  17. #include <asm/mipsregs.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/time.h>
  20. #include <asm/addrspace.h>
  21. #include <asm/smtc.h>
  22. #include <asm/smtc_ipi.h>
  23. #include <asm/smtc_proc.h>
  24. /*
  25. * SMTC Kernel needs to manipulate low-level CPU interrupt mask
  26. * in do_IRQ. These are passed in setup_irq_smtc() and stored
  27. * in this table.
  28. */
  29. unsigned long irq_hwmask[NR_IRQS];
  30. #define LOCK_MT_PRA() \
  31. local_irq_save(flags); \
  32. mtflags = dmt()
  33. #define UNLOCK_MT_PRA() \
  34. emt(mtflags); \
  35. local_irq_restore(flags)
  36. #define LOCK_CORE_PRA() \
  37. local_irq_save(flags); \
  38. mtflags = dvpe()
  39. #define UNLOCK_CORE_PRA() \
  40. evpe(mtflags); \
  41. local_irq_restore(flags)
  42. /*
  43. * Data structures purely associated with SMTC parallelism
  44. */
  45. /*
  46. * Table for tracking ASIDs whose lifetime is prolonged.
  47. */
  48. asiduse smtc_live_asid[MAX_SMTC_TLBS][MAX_SMTC_ASIDS];
  49. /*
  50. * Clock interrupt "latch" buffers, per "CPU"
  51. */
  52. static atomic_t ipi_timer_latch[NR_CPUS];
  53. /*
  54. * Number of InterProcessor Interrupt (IPI) message buffers to allocate
  55. */
  56. #define IPIBUF_PER_CPU 4
  57. static struct smtc_ipi_q IPIQ[NR_CPUS];
  58. static struct smtc_ipi_q freeIPIq;
  59. /* Forward declarations */
  60. void ipi_decode(struct smtc_ipi *);
  61. static void post_direct_ipi(int cpu, struct smtc_ipi *pipi);
  62. static void setup_cross_vpe_interrupts(unsigned int nvpe);
  63. void init_smtc_stats(void);
  64. /* Global SMTC Status */
  65. unsigned int smtc_status = 0;
  66. /* Boot command line configuration overrides */
  67. static int vpe0limit;
  68. static int ipibuffers = 0;
  69. static int nostlb = 0;
  70. static int asidmask = 0;
  71. unsigned long smtc_asid_mask = 0xff;
  72. static int __init vpe0tcs(char *str)
  73. {
  74. get_option(&str, &vpe0limit);
  75. return 1;
  76. }
  77. static int __init ipibufs(char *str)
  78. {
  79. get_option(&str, &ipibuffers);
  80. return 1;
  81. }
  82. static int __init stlb_disable(char *s)
  83. {
  84. nostlb = 1;
  85. return 1;
  86. }
  87. static int __init asidmask_set(char *str)
  88. {
  89. get_option(&str, &asidmask);
  90. switch (asidmask) {
  91. case 0x1:
  92. case 0x3:
  93. case 0x7:
  94. case 0xf:
  95. case 0x1f:
  96. case 0x3f:
  97. case 0x7f:
  98. case 0xff:
  99. smtc_asid_mask = (unsigned long)asidmask;
  100. break;
  101. default:
  102. printk("ILLEGAL ASID mask 0x%x from command line\n", asidmask);
  103. }
  104. return 1;
  105. }
  106. __setup("vpe0tcs=", vpe0tcs);
  107. __setup("ipibufs=", ipibufs);
  108. __setup("nostlb", stlb_disable);
  109. __setup("asidmask=", asidmask_set);
  110. #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
  111. static int hang_trig = 0;
  112. static int __init hangtrig_enable(char *s)
  113. {
  114. hang_trig = 1;
  115. return 1;
  116. }
  117. __setup("hangtrig", hangtrig_enable);
  118. #define DEFAULT_BLOCKED_IPI_LIMIT 32
  119. static int timerq_limit = DEFAULT_BLOCKED_IPI_LIMIT;
  120. static int __init tintq(char *str)
  121. {
  122. get_option(&str, &timerq_limit);
  123. return 1;
  124. }
  125. __setup("tintq=", tintq);
  126. static int imstuckcount[2][8];
  127. /* vpemask represents IM/IE bits of per-VPE Status registers, low-to-high */
  128. static int vpemask[2][8] = {
  129. {0, 0, 1, 0, 0, 0, 0, 1},
  130. {0, 0, 0, 0, 0, 0, 0, 1}
  131. };
  132. int tcnoprog[NR_CPUS];
  133. static atomic_t idle_hook_initialized = {0};
  134. static int clock_hang_reported[NR_CPUS];
  135. #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
  136. /*
  137. * Configure shared TLB - VPC configuration bit must be set by caller
  138. */
  139. static void smtc_configure_tlb(void)
  140. {
  141. int i, tlbsiz, vpes;
  142. unsigned long mvpconf0;
  143. unsigned long config1val;
  144. /* Set up ASID preservation table */
  145. for (vpes=0; vpes<MAX_SMTC_TLBS; vpes++) {
  146. for(i = 0; i < MAX_SMTC_ASIDS; i++) {
  147. smtc_live_asid[vpes][i] = 0;
  148. }
  149. }
  150. mvpconf0 = read_c0_mvpconf0();
  151. if ((vpes = ((mvpconf0 & MVPCONF0_PVPE)
  152. >> MVPCONF0_PVPE_SHIFT) + 1) > 1) {
  153. /* If we have multiple VPEs, try to share the TLB */
  154. if ((mvpconf0 & MVPCONF0_TLBS) && !nostlb) {
  155. /*
  156. * If TLB sizing is programmable, shared TLB
  157. * size is the total available complement.
  158. * Otherwise, we have to take the sum of all
  159. * static VPE TLB entries.
  160. */
  161. if ((tlbsiz = ((mvpconf0 & MVPCONF0_PTLBE)
  162. >> MVPCONF0_PTLBE_SHIFT)) == 0) {
  163. /*
  164. * If there's more than one VPE, there had better
  165. * be more than one TC, because we need one to bind
  166. * to each VPE in turn to be able to read
  167. * its configuration state!
  168. */
  169. settc(1);
  170. /* Stop the TC from doing anything foolish */
  171. write_tc_c0_tchalt(TCHALT_H);
  172. mips_ihb();
  173. /* No need to un-Halt - that happens later anyway */
  174. for (i=0; i < vpes; i++) {
  175. write_tc_c0_tcbind(i);
  176. /*
  177. * To be 100% sure we're really getting the right
  178. * information, we exit the configuration state
  179. * and do an IHB after each rebinding.
  180. */
  181. write_c0_mvpcontrol(
  182. read_c0_mvpcontrol() & ~ MVPCONTROL_VPC );
  183. mips_ihb();
  184. /*
  185. * Only count if the MMU Type indicated is TLB
  186. */
  187. if (((read_vpe_c0_config() & MIPS_CONF_MT) >> 7) == 1) {
  188. config1val = read_vpe_c0_config1();
  189. tlbsiz += ((config1val >> 25) & 0x3f) + 1;
  190. }
  191. /* Put core back in configuration state */
  192. write_c0_mvpcontrol(
  193. read_c0_mvpcontrol() | MVPCONTROL_VPC );
  194. mips_ihb();
  195. }
  196. }
  197. write_c0_mvpcontrol(read_c0_mvpcontrol() | MVPCONTROL_STLB);
  198. ehb();
  199. /*
  200. * Setup kernel data structures to use software total,
  201. * rather than read the per-VPE Config1 value. The values
  202. * for "CPU 0" gets copied to all the other CPUs as part
  203. * of their initialization in smtc_cpu_setup().
  204. */
  205. /* MIPS32 limits TLB indices to 64 */
  206. if (tlbsiz > 64)
  207. tlbsiz = 64;
  208. cpu_data[0].tlbsize = current_cpu_data.tlbsize = tlbsiz;
  209. smtc_status |= SMTC_TLB_SHARED;
  210. local_flush_tlb_all();
  211. printk("TLB of %d entry pairs shared by %d VPEs\n",
  212. tlbsiz, vpes);
  213. } else {
  214. printk("WARNING: TLB Not Sharable on SMTC Boot!\n");
  215. }
  216. }
  217. }
  218. /*
  219. * Incrementally build the CPU map out of constituent MIPS MT cores,
  220. * using the specified available VPEs and TCs. Plaform code needs
  221. * to ensure that each MIPS MT core invokes this routine on reset,
  222. * one at a time(!).
  223. *
  224. * This version of the build_cpu_map and prepare_cpus routines assumes
  225. * that *all* TCs of a MIPS MT core will be used for Linux, and that
  226. * they will be spread across *all* available VPEs (to minimise the
  227. * loss of efficiency due to exception service serialization).
  228. * An improved version would pick up configuration information and
  229. * possibly leave some TCs/VPEs as "slave" processors.
  230. *
  231. * Use c0_MVPConf0 to find out how many TCs are available, setting up
  232. * phys_cpu_present_map and the logical/physical mappings.
  233. */
  234. int __init mipsmt_build_cpu_map(int start_cpu_slot)
  235. {
  236. int i, ntcs;
  237. /*
  238. * The CPU map isn't actually used for anything at this point,
  239. * so it's not clear what else we should do apart from set
  240. * everything up so that "logical" = "physical".
  241. */
  242. ntcs = ((read_c0_mvpconf0() & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
  243. for (i=start_cpu_slot; i<NR_CPUS && i<ntcs; i++) {
  244. cpu_set(i, phys_cpu_present_map);
  245. __cpu_number_map[i] = i;
  246. __cpu_logical_map[i] = i;
  247. }
  248. #ifdef CONFIG_MIPS_MT_FPAFF
  249. /* Initialize map of CPUs with FPUs */
  250. cpus_clear(mt_fpu_cpumask);
  251. #endif
  252. /* One of those TC's is the one booting, and not a secondary... */
  253. printk("%i available secondary CPU TC(s)\n", i - 1);
  254. return i;
  255. }
  256. /*
  257. * Common setup before any secondaries are started
  258. * Make sure all CPU's are in a sensible state before we boot any of the
  259. * secondaries.
  260. *
  261. * For MIPS MT "SMTC" operation, we set up all TCs, spread as evenly
  262. * as possible across the available VPEs.
  263. */
  264. static void smtc_tc_setup(int vpe, int tc, int cpu)
  265. {
  266. settc(tc);
  267. write_tc_c0_tchalt(TCHALT_H);
  268. mips_ihb();
  269. write_tc_c0_tcstatus((read_tc_c0_tcstatus()
  270. & ~(TCSTATUS_TKSU | TCSTATUS_DA | TCSTATUS_IXMT))
  271. | TCSTATUS_A);
  272. write_tc_c0_tccontext(0);
  273. /* Bind tc to vpe */
  274. write_tc_c0_tcbind(vpe);
  275. /* In general, all TCs should have the same cpu_data indications */
  276. memcpy(&cpu_data[cpu], &cpu_data[0], sizeof(struct cpuinfo_mips));
  277. /* For 34Kf, start with TC/CPU 0 as sole owner of single FPU context */
  278. if (cpu_data[0].cputype == CPU_34K ||
  279. cpu_data[0].cputype == CPU_1004K)
  280. cpu_data[cpu].options &= ~MIPS_CPU_FPU;
  281. cpu_data[cpu].vpe_id = vpe;
  282. cpu_data[cpu].tc_id = tc;
  283. }
  284. void mipsmt_prepare_cpus(void)
  285. {
  286. int i, vpe, tc, ntc, nvpe, tcpervpe[NR_CPUS], slop, cpu;
  287. unsigned long flags;
  288. unsigned long val;
  289. int nipi;
  290. struct smtc_ipi *pipi;
  291. /* disable interrupts so we can disable MT */
  292. local_irq_save(flags);
  293. /* disable MT so we can configure */
  294. dvpe();
  295. dmt();
  296. spin_lock_init(&freeIPIq.lock);
  297. /*
  298. * We probably don't have as many VPEs as we do SMP "CPUs",
  299. * but it's possible - and in any case we'll never use more!
  300. */
  301. for (i=0; i<NR_CPUS; i++) {
  302. IPIQ[i].head = IPIQ[i].tail = NULL;
  303. spin_lock_init(&IPIQ[i].lock);
  304. IPIQ[i].depth = 0;
  305. atomic_set(&ipi_timer_latch[i], 0);
  306. }
  307. /* cpu_data index starts at zero */
  308. cpu = 0;
  309. cpu_data[cpu].vpe_id = 0;
  310. cpu_data[cpu].tc_id = 0;
  311. cpu++;
  312. /* Report on boot-time options */
  313. mips_mt_set_cpuoptions();
  314. if (vpelimit > 0)
  315. printk("Limit of %d VPEs set\n", vpelimit);
  316. if (tclimit > 0)
  317. printk("Limit of %d TCs set\n", tclimit);
  318. if (nostlb) {
  319. printk("Shared TLB Use Inhibited - UNSAFE for Multi-VPE Operation\n");
  320. }
  321. if (asidmask)
  322. printk("ASID mask value override to 0x%x\n", asidmask);
  323. /* Temporary */
  324. #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
  325. if (hang_trig)
  326. printk("Logic Analyser Trigger on suspected TC hang\n");
  327. #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
  328. /* Put MVPE's into 'configuration state' */
  329. write_c0_mvpcontrol( read_c0_mvpcontrol() | MVPCONTROL_VPC );
  330. val = read_c0_mvpconf0();
  331. nvpe = ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1;
  332. if (vpelimit > 0 && nvpe > vpelimit)
  333. nvpe = vpelimit;
  334. ntc = ((val & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
  335. if (ntc > NR_CPUS)
  336. ntc = NR_CPUS;
  337. if (tclimit > 0 && ntc > tclimit)
  338. ntc = tclimit;
  339. slop = ntc % nvpe;
  340. for (i = 0; i < nvpe; i++) {
  341. tcpervpe[i] = ntc / nvpe;
  342. if (slop) {
  343. if((slop - i) > 0) tcpervpe[i]++;
  344. }
  345. }
  346. /* Handle command line override for VPE0 */
  347. if (vpe0limit > ntc) vpe0limit = ntc;
  348. if (vpe0limit > 0) {
  349. int slopslop;
  350. if (vpe0limit < tcpervpe[0]) {
  351. /* Reducing TC count - distribute to others */
  352. slop = tcpervpe[0] - vpe0limit;
  353. slopslop = slop % (nvpe - 1);
  354. tcpervpe[0] = vpe0limit;
  355. for (i = 1; i < nvpe; i++) {
  356. tcpervpe[i] += slop / (nvpe - 1);
  357. if(slopslop && ((slopslop - (i - 1) > 0)))
  358. tcpervpe[i]++;
  359. }
  360. } else if (vpe0limit > tcpervpe[0]) {
  361. /* Increasing TC count - steal from others */
  362. slop = vpe0limit - tcpervpe[0];
  363. slopslop = slop % (nvpe - 1);
  364. tcpervpe[0] = vpe0limit;
  365. for (i = 1; i < nvpe; i++) {
  366. tcpervpe[i] -= slop / (nvpe - 1);
  367. if(slopslop && ((slopslop - (i - 1) > 0)))
  368. tcpervpe[i]--;
  369. }
  370. }
  371. }
  372. /* Set up shared TLB */
  373. smtc_configure_tlb();
  374. for (tc = 0, vpe = 0 ; (vpe < nvpe) && (tc < ntc) ; vpe++) {
  375. /*
  376. * Set the MVP bits.
  377. */
  378. settc(tc);
  379. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_MVP);
  380. if (vpe != 0)
  381. printk(", ");
  382. printk("VPE %d: TC", vpe);
  383. for (i = 0; i < tcpervpe[vpe]; i++) {
  384. /*
  385. * TC 0 is bound to VPE 0 at reset,
  386. * and is presumably executing this
  387. * code. Leave it alone!
  388. */
  389. if (tc != 0) {
  390. smtc_tc_setup(vpe, tc, cpu);
  391. cpu++;
  392. }
  393. printk(" %d", tc);
  394. tc++;
  395. }
  396. if (vpe != 0) {
  397. /*
  398. * Clear any stale software interrupts from VPE's Cause
  399. */
  400. write_vpe_c0_cause(0);
  401. /*
  402. * Clear ERL/EXL of VPEs other than 0
  403. * and set restricted interrupt enable/mask.
  404. */
  405. write_vpe_c0_status((read_vpe_c0_status()
  406. & ~(ST0_BEV | ST0_ERL | ST0_EXL | ST0_IM))
  407. | (STATUSF_IP0 | STATUSF_IP1 | STATUSF_IP7
  408. | ST0_IE));
  409. /*
  410. * set config to be the same as vpe0,
  411. * particularly kseg0 coherency alg
  412. */
  413. write_vpe_c0_config(read_c0_config());
  414. /* Clear any pending timer interrupt */
  415. write_vpe_c0_compare(0);
  416. /* Propagate Config7 */
  417. write_vpe_c0_config7(read_c0_config7());
  418. write_vpe_c0_count(read_c0_count());
  419. }
  420. /* enable multi-threading within VPE */
  421. write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() | VPECONTROL_TE);
  422. /* enable the VPE */
  423. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA);
  424. }
  425. /*
  426. * Pull any physically present but unused TCs out of circulation.
  427. */
  428. while (tc < (((val & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1)) {
  429. cpu_clear(tc, phys_cpu_present_map);
  430. cpu_clear(tc, cpu_present_map);
  431. tc++;
  432. }
  433. /* release config state */
  434. write_c0_mvpcontrol( read_c0_mvpcontrol() & ~ MVPCONTROL_VPC );
  435. printk("\n");
  436. /* Set up coprocessor affinity CPU mask(s) */
  437. #ifdef CONFIG_MIPS_MT_FPAFF
  438. for (tc = 0; tc < ntc; tc++) {
  439. if (cpu_data[tc].options & MIPS_CPU_FPU)
  440. cpu_set(tc, mt_fpu_cpumask);
  441. }
  442. #endif
  443. /* set up ipi interrupts... */
  444. /* If we have multiple VPEs running, set up the cross-VPE interrupt */
  445. setup_cross_vpe_interrupts(nvpe);
  446. /* Set up queue of free IPI "messages". */
  447. nipi = NR_CPUS * IPIBUF_PER_CPU;
  448. if (ipibuffers > 0)
  449. nipi = ipibuffers;
  450. pipi = kmalloc(nipi *sizeof(struct smtc_ipi), GFP_KERNEL);
  451. if (pipi == NULL)
  452. panic("kmalloc of IPI message buffers failed\n");
  453. else
  454. printk("IPI buffer pool of %d buffers\n", nipi);
  455. for (i = 0; i < nipi; i++) {
  456. smtc_ipi_nq(&freeIPIq, pipi);
  457. pipi++;
  458. }
  459. /* Arm multithreading and enable other VPEs - but all TCs are Halted */
  460. emt(EMT_ENABLE);
  461. evpe(EVPE_ENABLE);
  462. local_irq_restore(flags);
  463. /* Initialize SMTC /proc statistics/diagnostics */
  464. init_smtc_stats();
  465. }
  466. /*
  467. * Setup the PC, SP, and GP of a secondary processor and start it
  468. * running!
  469. * smp_bootstrap is the place to resume from
  470. * __KSTK_TOS(idle) is apparently the stack pointer
  471. * (unsigned long)idle->thread_info the gp
  472. *
  473. */
  474. void __cpuinit smtc_boot_secondary(int cpu, struct task_struct *idle)
  475. {
  476. extern u32 kernelsp[NR_CPUS];
  477. long flags;
  478. int mtflags;
  479. LOCK_MT_PRA();
  480. if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
  481. dvpe();
  482. }
  483. settc(cpu_data[cpu].tc_id);
  484. /* pc */
  485. write_tc_c0_tcrestart((unsigned long)&smp_bootstrap);
  486. /* stack pointer */
  487. kernelsp[cpu] = __KSTK_TOS(idle);
  488. write_tc_gpr_sp(__KSTK_TOS(idle));
  489. /* global pointer */
  490. write_tc_gpr_gp((unsigned long)task_thread_info(idle));
  491. smtc_status |= SMTC_MTC_ACTIVE;
  492. write_tc_c0_tchalt(0);
  493. if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
  494. evpe(EVPE_ENABLE);
  495. }
  496. UNLOCK_MT_PRA();
  497. }
  498. void smtc_init_secondary(void)
  499. {
  500. /*
  501. * Start timer on secondary VPEs if necessary.
  502. * plat_timer_setup has already have been invoked by init/main
  503. * on "boot" TC. Like per_cpu_trap_init() hack, this assumes that
  504. * SMTC init code assigns TCs consdecutively and in ascending order
  505. * to across available VPEs.
  506. */
  507. if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
  508. ((read_c0_tcbind() & TCBIND_CURVPE)
  509. != cpu_data[smp_processor_id() - 1].vpe_id)){
  510. write_c0_compare(read_c0_count() + mips_hpt_frequency/HZ);
  511. }
  512. local_irq_enable();
  513. }
  514. void smtc_smp_finish(void)
  515. {
  516. printk("TC %d going on-line as CPU %d\n",
  517. cpu_data[smp_processor_id()].tc_id, smp_processor_id());
  518. }
  519. void smtc_cpus_done(void)
  520. {
  521. }
  522. /*
  523. * Support for SMTC-optimized driver IRQ registration
  524. */
  525. /*
  526. * SMTC Kernel needs to manipulate low-level CPU interrupt mask
  527. * in do_IRQ. These are passed in setup_irq_smtc() and stored
  528. * in this table.
  529. */
  530. int setup_irq_smtc(unsigned int irq, struct irqaction * new,
  531. unsigned long hwmask)
  532. {
  533. #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
  534. unsigned int vpe = current_cpu_data.vpe_id;
  535. vpemask[vpe][irq - MIPS_CPU_IRQ_BASE] = 1;
  536. #endif
  537. irq_hwmask[irq] = hwmask;
  538. return setup_irq(irq, new);
  539. }
  540. #ifdef CONFIG_MIPS_MT_SMTC_IRQAFF
  541. /*
  542. * Support for IRQ affinity to TCs
  543. */
  544. void smtc_set_irq_affinity(unsigned int irq, cpumask_t affinity)
  545. {
  546. /*
  547. * If a "fast path" cache of quickly decodable affinity state
  548. * is maintained, this is where it gets done, on a call up
  549. * from the platform affinity code.
  550. */
  551. }
  552. void smtc_forward_irq(unsigned int irq)
  553. {
  554. int target;
  555. /*
  556. * OK wise guy, now figure out how to get the IRQ
  557. * to be serviced on an authorized "CPU".
  558. *
  559. * Ideally, to handle the situation where an IRQ has multiple
  560. * eligible CPUS, we would maintain state per IRQ that would
  561. * allow a fair distribution of service requests. Since the
  562. * expected use model is any-or-only-one, for simplicity
  563. * and efficiency, we just pick the easiest one to find.
  564. */
  565. target = first_cpu(irq_desc[irq].affinity);
  566. /*
  567. * We depend on the platform code to have correctly processed
  568. * IRQ affinity change requests to ensure that the IRQ affinity
  569. * mask has been purged of bits corresponding to nonexistent and
  570. * offline "CPUs", and to TCs bound to VPEs other than the VPE
  571. * connected to the physical interrupt input for the interrupt
  572. * in question. Otherwise we have a nasty problem with interrupt
  573. * mask management. This is best handled in non-performance-critical
  574. * platform IRQ affinity setting code, to minimize interrupt-time
  575. * checks.
  576. */
  577. /* If no one is eligible, service locally */
  578. if (target >= NR_CPUS) {
  579. do_IRQ_no_affinity(irq);
  580. return;
  581. }
  582. smtc_send_ipi(target, IRQ_AFFINITY_IPI, irq);
  583. }
  584. #endif /* CONFIG_MIPS_MT_SMTC_IRQAFF */
  585. /*
  586. * IPI model for SMTC is tricky, because interrupts aren't TC-specific.
  587. * Within a VPE one TC can interrupt another by different approaches.
  588. * The easiest to get right would probably be to make all TCs except
  589. * the target IXMT and set a software interrupt, but an IXMT-based
  590. * scheme requires that a handler must run before a new IPI could
  591. * be sent, which would break the "broadcast" loops in MIPS MT.
  592. * A more gonzo approach within a VPE is to halt the TC, extract
  593. * its Restart, Status, and a couple of GPRs, and program the Restart
  594. * address to emulate an interrupt.
  595. *
  596. * Within a VPE, one can be confident that the target TC isn't in
  597. * a critical EXL state when halted, since the write to the Halt
  598. * register could not have issued on the writing thread if the
  599. * halting thread had EXL set. So k0 and k1 of the target TC
  600. * can be used by the injection code. Across VPEs, one can't
  601. * be certain that the target TC isn't in a critical exception
  602. * state. So we try a two-step process of sending a software
  603. * interrupt to the target VPE, which either handles the event
  604. * itself (if it was the target) or injects the event within
  605. * the VPE.
  606. */
  607. static void smtc_ipi_qdump(void)
  608. {
  609. int i;
  610. for (i = 0; i < NR_CPUS ;i++) {
  611. printk("IPIQ[%d]: head = 0x%x, tail = 0x%x, depth = %d\n",
  612. i, (unsigned)IPIQ[i].head, (unsigned)IPIQ[i].tail,
  613. IPIQ[i].depth);
  614. }
  615. }
  616. /*
  617. * The standard atomic.h primitives don't quite do what we want
  618. * here: We need an atomic add-and-return-previous-value (which
  619. * could be done with atomic_add_return and a decrement) and an
  620. * atomic set/zero-and-return-previous-value (which can't really
  621. * be done with the atomic.h primitives). And since this is
  622. * MIPS MT, we can assume that we have LL/SC.
  623. */
  624. static inline int atomic_postincrement(atomic_t *v)
  625. {
  626. unsigned long result;
  627. unsigned long temp;
  628. __asm__ __volatile__(
  629. "1: ll %0, %2 \n"
  630. " addu %1, %0, 1 \n"
  631. " sc %1, %2 \n"
  632. " beqz %1, 1b \n"
  633. __WEAK_LLSC_MB
  634. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  635. : "m" (v->counter)
  636. : "memory");
  637. return result;
  638. }
  639. void smtc_send_ipi(int cpu, int type, unsigned int action)
  640. {
  641. int tcstatus;
  642. struct smtc_ipi *pipi;
  643. long flags;
  644. int mtflags;
  645. if (cpu == smp_processor_id()) {
  646. printk("Cannot Send IPI to self!\n");
  647. return;
  648. }
  649. /* Set up a descriptor, to be delivered either promptly or queued */
  650. pipi = smtc_ipi_dq(&freeIPIq);
  651. if (pipi == NULL) {
  652. bust_spinlocks(1);
  653. mips_mt_regdump(dvpe());
  654. panic("IPI Msg. Buffers Depleted\n");
  655. }
  656. pipi->type = type;
  657. pipi->arg = (void *)action;
  658. pipi->dest = cpu;
  659. if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
  660. if (type == SMTC_CLOCK_TICK)
  661. atomic_inc(&ipi_timer_latch[cpu]);
  662. /* If not on same VPE, enqueue and send cross-VPE interrupt */
  663. smtc_ipi_nq(&IPIQ[cpu], pipi);
  664. LOCK_CORE_PRA();
  665. settc(cpu_data[cpu].tc_id);
  666. write_vpe_c0_cause(read_vpe_c0_cause() | C_SW1);
  667. UNLOCK_CORE_PRA();
  668. } else {
  669. /*
  670. * Not sufficient to do a LOCK_MT_PRA (dmt) here,
  671. * since ASID shootdown on the other VPE may
  672. * collide with this operation.
  673. */
  674. LOCK_CORE_PRA();
  675. settc(cpu_data[cpu].tc_id);
  676. /* Halt the targeted TC */
  677. write_tc_c0_tchalt(TCHALT_H);
  678. mips_ihb();
  679. /*
  680. * Inspect TCStatus - if IXMT is set, we have to queue
  681. * a message. Otherwise, we set up the "interrupt"
  682. * of the other TC
  683. */
  684. tcstatus = read_tc_c0_tcstatus();
  685. if ((tcstatus & TCSTATUS_IXMT) != 0) {
  686. /*
  687. * Spin-waiting here can deadlock,
  688. * so we queue the message for the target TC.
  689. */
  690. write_tc_c0_tchalt(0);
  691. UNLOCK_CORE_PRA();
  692. /* Try to reduce redundant timer interrupt messages */
  693. if (type == SMTC_CLOCK_TICK) {
  694. if (atomic_postincrement(&ipi_timer_latch[cpu])!=0){
  695. smtc_ipi_nq(&freeIPIq, pipi);
  696. return;
  697. }
  698. }
  699. smtc_ipi_nq(&IPIQ[cpu], pipi);
  700. } else {
  701. if (type == SMTC_CLOCK_TICK)
  702. atomic_inc(&ipi_timer_latch[cpu]);
  703. post_direct_ipi(cpu, pipi);
  704. write_tc_c0_tchalt(0);
  705. UNLOCK_CORE_PRA();
  706. }
  707. }
  708. }
  709. /*
  710. * Send IPI message to Halted TC, TargTC/TargVPE already having been set
  711. */
  712. static void post_direct_ipi(int cpu, struct smtc_ipi *pipi)
  713. {
  714. struct pt_regs *kstack;
  715. unsigned long tcstatus;
  716. unsigned long tcrestart;
  717. extern u32 kernelsp[NR_CPUS];
  718. extern void __smtc_ipi_vector(void);
  719. //printk("%s: on %d for %d\n", __func__, smp_processor_id(), cpu);
  720. /* Extract Status, EPC from halted TC */
  721. tcstatus = read_tc_c0_tcstatus();
  722. tcrestart = read_tc_c0_tcrestart();
  723. /* If TCRestart indicates a WAIT instruction, advance the PC */
  724. if ((tcrestart & 0x80000000)
  725. && ((*(unsigned int *)tcrestart & 0xfe00003f) == 0x42000020)) {
  726. tcrestart += 4;
  727. }
  728. /*
  729. * Save on TC's future kernel stack
  730. *
  731. * CU bit of Status is indicator that TC was
  732. * already running on a kernel stack...
  733. */
  734. if (tcstatus & ST0_CU0) {
  735. /* Note that this "- 1" is pointer arithmetic */
  736. kstack = ((struct pt_regs *)read_tc_gpr_sp()) - 1;
  737. } else {
  738. kstack = ((struct pt_regs *)kernelsp[cpu]) - 1;
  739. }
  740. kstack->cp0_epc = (long)tcrestart;
  741. /* Save TCStatus */
  742. kstack->cp0_tcstatus = tcstatus;
  743. /* Pass token of operation to be performed kernel stack pad area */
  744. kstack->pad0[4] = (unsigned long)pipi;
  745. /* Pass address of function to be called likewise */
  746. kstack->pad0[5] = (unsigned long)&ipi_decode;
  747. /* Set interrupt exempt and kernel mode */
  748. tcstatus |= TCSTATUS_IXMT;
  749. tcstatus &= ~TCSTATUS_TKSU;
  750. write_tc_c0_tcstatus(tcstatus);
  751. ehb();
  752. /* Set TC Restart address to be SMTC IPI vector */
  753. write_tc_c0_tcrestart(__smtc_ipi_vector);
  754. }
  755. static void ipi_resched_interrupt(void)
  756. {
  757. /* Return from interrupt should be enough to cause scheduler check */
  758. }
  759. static void ipi_call_interrupt(void)
  760. {
  761. /* Invoke generic function invocation code in smp.c */
  762. smp_call_function_interrupt();
  763. }
  764. DECLARE_PER_CPU(struct clock_event_device, smtc_dummy_clockevent_device);
  765. void ipi_decode(struct smtc_ipi *pipi)
  766. {
  767. unsigned int cpu = smp_processor_id();
  768. struct clock_event_device *cd;
  769. void *arg_copy = pipi->arg;
  770. int type_copy = pipi->type;
  771. int ticks;
  772. smtc_ipi_nq(&freeIPIq, pipi);
  773. switch (type_copy) {
  774. case SMTC_CLOCK_TICK:
  775. irq_enter();
  776. kstat_this_cpu.irqs[MIPS_CPU_IRQ_BASE + 1]++;
  777. cd = &per_cpu(smtc_dummy_clockevent_device, cpu);
  778. ticks = atomic_read(&ipi_timer_latch[cpu]);
  779. atomic_sub(ticks, &ipi_timer_latch[cpu]);
  780. while (ticks) {
  781. cd->event_handler(cd);
  782. ticks--;
  783. }
  784. irq_exit();
  785. break;
  786. case LINUX_SMP_IPI:
  787. switch ((int)arg_copy) {
  788. case SMP_RESCHEDULE_YOURSELF:
  789. ipi_resched_interrupt();
  790. break;
  791. case SMP_CALL_FUNCTION:
  792. ipi_call_interrupt();
  793. break;
  794. default:
  795. printk("Impossible SMTC IPI Argument 0x%x\n",
  796. (int)arg_copy);
  797. break;
  798. }
  799. break;
  800. #ifdef CONFIG_MIPS_MT_SMTC_IRQAFF
  801. case IRQ_AFFINITY_IPI:
  802. /*
  803. * Accept a "forwarded" interrupt that was initially
  804. * taken by a TC who doesn't have affinity for the IRQ.
  805. */
  806. do_IRQ_no_affinity((int)arg_copy);
  807. break;
  808. #endif /* CONFIG_MIPS_MT_SMTC_IRQAFF */
  809. default:
  810. printk("Impossible SMTC IPI Type 0x%x\n", type_copy);
  811. break;
  812. }
  813. }
  814. void deferred_smtc_ipi(void)
  815. {
  816. struct smtc_ipi *pipi;
  817. unsigned long flags;
  818. /* DEBUG */
  819. int q = smp_processor_id();
  820. /*
  821. * Test is not atomic, but much faster than a dequeue,
  822. * and the vast majority of invocations will have a null queue.
  823. */
  824. if (IPIQ[q].head != NULL) {
  825. while((pipi = smtc_ipi_dq(&IPIQ[q])) != NULL) {
  826. /* ipi_decode() should be called with interrupts off */
  827. local_irq_save(flags);
  828. ipi_decode(pipi);
  829. local_irq_restore(flags);
  830. }
  831. }
  832. }
  833. /*
  834. * Cross-VPE interrupts in the SMTC prototype use "software interrupts"
  835. * set via cross-VPE MTTR manipulation of the Cause register. It would be
  836. * in some regards preferable to have external logic for "doorbell" hardware
  837. * interrupts.
  838. */
  839. static int cpu_ipi_irq = MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_IRQ;
  840. static irqreturn_t ipi_interrupt(int irq, void *dev_idm)
  841. {
  842. int my_vpe = cpu_data[smp_processor_id()].vpe_id;
  843. int my_tc = cpu_data[smp_processor_id()].tc_id;
  844. int cpu;
  845. struct smtc_ipi *pipi;
  846. unsigned long tcstatus;
  847. int sent;
  848. long flags;
  849. unsigned int mtflags;
  850. unsigned int vpflags;
  851. /*
  852. * So long as cross-VPE interrupts are done via
  853. * MFTR/MTTR read-modify-writes of Cause, we need
  854. * to stop other VPEs whenever the local VPE does
  855. * anything similar.
  856. */
  857. local_irq_save(flags);
  858. vpflags = dvpe();
  859. clear_c0_cause(0x100 << MIPS_CPU_IPI_IRQ);
  860. set_c0_status(0x100 << MIPS_CPU_IPI_IRQ);
  861. irq_enable_hazard();
  862. evpe(vpflags);
  863. local_irq_restore(flags);
  864. /*
  865. * Cross-VPE Interrupt handler: Try to directly deliver IPIs
  866. * queued for TCs on this VPE other than the current one.
  867. * Return-from-interrupt should cause us to drain the queue
  868. * for the current TC, so we ought not to have to do it explicitly here.
  869. */
  870. for_each_online_cpu(cpu) {
  871. if (cpu_data[cpu].vpe_id != my_vpe)
  872. continue;
  873. pipi = smtc_ipi_dq(&IPIQ[cpu]);
  874. if (pipi != NULL) {
  875. if (cpu_data[cpu].tc_id != my_tc) {
  876. sent = 0;
  877. LOCK_MT_PRA();
  878. settc(cpu_data[cpu].tc_id);
  879. write_tc_c0_tchalt(TCHALT_H);
  880. mips_ihb();
  881. tcstatus = read_tc_c0_tcstatus();
  882. if ((tcstatus & TCSTATUS_IXMT) == 0) {
  883. post_direct_ipi(cpu, pipi);
  884. sent = 1;
  885. }
  886. write_tc_c0_tchalt(0);
  887. UNLOCK_MT_PRA();
  888. if (!sent) {
  889. smtc_ipi_req(&IPIQ[cpu], pipi);
  890. }
  891. } else {
  892. /*
  893. * ipi_decode() should be called
  894. * with interrupts off
  895. */
  896. local_irq_save(flags);
  897. ipi_decode(pipi);
  898. local_irq_restore(flags);
  899. }
  900. }
  901. }
  902. return IRQ_HANDLED;
  903. }
  904. static void ipi_irq_dispatch(void)
  905. {
  906. do_IRQ(cpu_ipi_irq);
  907. }
  908. static struct irqaction irq_ipi = {
  909. .handler = ipi_interrupt,
  910. .flags = IRQF_DISABLED,
  911. .name = "SMTC_IPI",
  912. .flags = IRQF_PERCPU
  913. };
  914. static void setup_cross_vpe_interrupts(unsigned int nvpe)
  915. {
  916. if (nvpe < 1)
  917. return;
  918. if (!cpu_has_vint)
  919. panic("SMTC Kernel requires Vectored Interrupt support");
  920. set_vi_handler(MIPS_CPU_IPI_IRQ, ipi_irq_dispatch);
  921. setup_irq_smtc(cpu_ipi_irq, &irq_ipi, (0x100 << MIPS_CPU_IPI_IRQ));
  922. set_irq_handler(cpu_ipi_irq, handle_percpu_irq);
  923. }
  924. /*
  925. * SMTC-specific hacks invoked from elsewhere in the kernel.
  926. *
  927. * smtc_ipi_replay is called from raw_local_irq_restore which is only ever
  928. * called with interrupts disabled. We do rely on interrupts being disabled
  929. * here because using spin_lock_irqsave()/spin_unlock_irqrestore() would
  930. * result in a recursive call to raw_local_irq_restore().
  931. */
  932. static void __smtc_ipi_replay(void)
  933. {
  934. unsigned int cpu = smp_processor_id();
  935. /*
  936. * To the extent that we've ever turned interrupts off,
  937. * we may have accumulated deferred IPIs. This is subtle.
  938. * If we use the smtc_ipi_qdepth() macro, we'll get an
  939. * exact number - but we'll also disable interrupts
  940. * and create a window of failure where a new IPI gets
  941. * queued after we test the depth but before we re-enable
  942. * interrupts. So long as IXMT never gets set, however,
  943. * we should be OK: If we pick up something and dispatch
  944. * it here, that's great. If we see nothing, but concurrent
  945. * with this operation, another TC sends us an IPI, IXMT
  946. * is clear, and we'll handle it as a real pseudo-interrupt
  947. * and not a pseudo-pseudo interrupt.
  948. */
  949. if (IPIQ[cpu].depth > 0) {
  950. while (1) {
  951. struct smtc_ipi_q *q = &IPIQ[cpu];
  952. struct smtc_ipi *pipi;
  953. extern void self_ipi(struct smtc_ipi *);
  954. spin_lock(&q->lock);
  955. pipi = __smtc_ipi_dq(q);
  956. spin_unlock(&q->lock);
  957. if (!pipi)
  958. break;
  959. self_ipi(pipi);
  960. smtc_cpu_stats[cpu].selfipis++;
  961. }
  962. }
  963. }
  964. void smtc_ipi_replay(void)
  965. {
  966. raw_local_irq_disable();
  967. __smtc_ipi_replay();
  968. }
  969. EXPORT_SYMBOL(smtc_ipi_replay);
  970. void smtc_idle_loop_hook(void)
  971. {
  972. #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
  973. int im;
  974. int flags;
  975. int mtflags;
  976. int bit;
  977. int vpe;
  978. int tc;
  979. int hook_ntcs;
  980. /*
  981. * printk within DMT-protected regions can deadlock,
  982. * so buffer diagnostic messages for later output.
  983. */
  984. char *pdb_msg;
  985. char id_ho_db_msg[768]; /* worst-case use should be less than 700 */
  986. if (atomic_read(&idle_hook_initialized) == 0) { /* fast test */
  987. if (atomic_add_return(1, &idle_hook_initialized) == 1) {
  988. int mvpconf0;
  989. /* Tedious stuff to just do once */
  990. mvpconf0 = read_c0_mvpconf0();
  991. hook_ntcs = ((mvpconf0 & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
  992. if (hook_ntcs > NR_CPUS)
  993. hook_ntcs = NR_CPUS;
  994. for (tc = 0; tc < hook_ntcs; tc++) {
  995. tcnoprog[tc] = 0;
  996. clock_hang_reported[tc] = 0;
  997. }
  998. for (vpe = 0; vpe < 2; vpe++)
  999. for (im = 0; im < 8; im++)
  1000. imstuckcount[vpe][im] = 0;
  1001. printk("Idle loop test hook initialized for %d TCs\n", hook_ntcs);
  1002. atomic_set(&idle_hook_initialized, 1000);
  1003. } else {
  1004. /* Someone else is initializing in parallel - let 'em finish */
  1005. while (atomic_read(&idle_hook_initialized) < 1000)
  1006. ;
  1007. }
  1008. }
  1009. /* Have we stupidly left IXMT set somewhere? */
  1010. if (read_c0_tcstatus() & 0x400) {
  1011. write_c0_tcstatus(read_c0_tcstatus() & ~0x400);
  1012. ehb();
  1013. printk("Dangling IXMT in cpu_idle()\n");
  1014. }
  1015. /* Have we stupidly left an IM bit turned off? */
  1016. #define IM_LIMIT 2000
  1017. local_irq_save(flags);
  1018. mtflags = dmt();
  1019. pdb_msg = &id_ho_db_msg[0];
  1020. im = read_c0_status();
  1021. vpe = current_cpu_data.vpe_id;
  1022. for (bit = 0; bit < 8; bit++) {
  1023. /*
  1024. * In current prototype, I/O interrupts
  1025. * are masked for VPE > 0
  1026. */
  1027. if (vpemask[vpe][bit]) {
  1028. if (!(im & (0x100 << bit)))
  1029. imstuckcount[vpe][bit]++;
  1030. else
  1031. imstuckcount[vpe][bit] = 0;
  1032. if (imstuckcount[vpe][bit] > IM_LIMIT) {
  1033. set_c0_status(0x100 << bit);
  1034. ehb();
  1035. imstuckcount[vpe][bit] = 0;
  1036. pdb_msg += sprintf(pdb_msg,
  1037. "Dangling IM %d fixed for VPE %d\n", bit,
  1038. vpe);
  1039. }
  1040. }
  1041. }
  1042. /*
  1043. * Now that we limit outstanding timer IPIs, check for hung TC
  1044. */
  1045. for (tc = 0; tc < NR_CPUS; tc++) {
  1046. /* Don't check ourself - we'll dequeue IPIs just below */
  1047. if ((tc != smp_processor_id()) &&
  1048. atomic_read(&ipi_timer_latch[tc]) > timerq_limit) {
  1049. if (clock_hang_reported[tc] == 0) {
  1050. pdb_msg += sprintf(pdb_msg,
  1051. "TC %d looks hung with timer latch at %d\n",
  1052. tc, atomic_read(&ipi_timer_latch[tc]));
  1053. clock_hang_reported[tc]++;
  1054. }
  1055. }
  1056. }
  1057. emt(mtflags);
  1058. local_irq_restore(flags);
  1059. if (pdb_msg != &id_ho_db_msg[0])
  1060. printk("CPU%d: %s", smp_processor_id(), id_ho_db_msg);
  1061. #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
  1062. /*
  1063. * Replay any accumulated deferred IPIs. If "Instant Replay"
  1064. * is in use, there should never be any.
  1065. */
  1066. #ifndef CONFIG_MIPS_MT_SMTC_INSTANT_REPLAY
  1067. {
  1068. unsigned long flags;
  1069. local_irq_save(flags);
  1070. __smtc_ipi_replay();
  1071. local_irq_restore(flags);
  1072. }
  1073. #endif /* CONFIG_MIPS_MT_SMTC_INSTANT_REPLAY */
  1074. }
  1075. void smtc_soft_dump(void)
  1076. {
  1077. int i;
  1078. printk("Counter Interrupts taken per CPU (TC)\n");
  1079. for (i=0; i < NR_CPUS; i++) {
  1080. printk("%d: %ld\n", i, smtc_cpu_stats[i].timerints);
  1081. }
  1082. printk("Self-IPI invocations:\n");
  1083. for (i=0; i < NR_CPUS; i++) {
  1084. printk("%d: %ld\n", i, smtc_cpu_stats[i].selfipis);
  1085. }
  1086. smtc_ipi_qdump();
  1087. printk("Timer IPI Backlogs:\n");
  1088. for (i=0; i < NR_CPUS; i++) {
  1089. printk("%d: %d\n", i, atomic_read(&ipi_timer_latch[i]));
  1090. }
  1091. printk("%d Recoveries of \"stolen\" FPU\n",
  1092. atomic_read(&smtc_fpu_recoveries));
  1093. }
  1094. /*
  1095. * TLB management routines special to SMTC
  1096. */
  1097. void smtc_get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
  1098. {
  1099. unsigned long flags, mtflags, tcstat, prevhalt, asid;
  1100. int tlb, i;
  1101. /*
  1102. * It would be nice to be able to use a spinlock here,
  1103. * but this is invoked from within TLB flush routines
  1104. * that protect themselves with DVPE, so if a lock is
  1105. * held by another TC, it'll never be freed.
  1106. *
  1107. * DVPE/DMT must not be done with interrupts enabled,
  1108. * so even so most callers will already have disabled
  1109. * them, let's be really careful...
  1110. */
  1111. local_irq_save(flags);
  1112. if (smtc_status & SMTC_TLB_SHARED) {
  1113. mtflags = dvpe();
  1114. tlb = 0;
  1115. } else {
  1116. mtflags = dmt();
  1117. tlb = cpu_data[cpu].vpe_id;
  1118. }
  1119. asid = asid_cache(cpu);
  1120. do {
  1121. if (!((asid += ASID_INC) & ASID_MASK) ) {
  1122. if (cpu_has_vtag_icache)
  1123. flush_icache_all();
  1124. /* Traverse all online CPUs (hack requires contigous range) */
  1125. for_each_online_cpu(i) {
  1126. /*
  1127. * We don't need to worry about our own CPU, nor those of
  1128. * CPUs who don't share our TLB.
  1129. */
  1130. if ((i != smp_processor_id()) &&
  1131. ((smtc_status & SMTC_TLB_SHARED) ||
  1132. (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))) {
  1133. settc(cpu_data[i].tc_id);
  1134. prevhalt = read_tc_c0_tchalt() & TCHALT_H;
  1135. if (!prevhalt) {
  1136. write_tc_c0_tchalt(TCHALT_H);
  1137. mips_ihb();
  1138. }
  1139. tcstat = read_tc_c0_tcstatus();
  1140. smtc_live_asid[tlb][(tcstat & ASID_MASK)] |= (asiduse)(0x1 << i);
  1141. if (!prevhalt)
  1142. write_tc_c0_tchalt(0);
  1143. }
  1144. }
  1145. if (!asid) /* fix version if needed */
  1146. asid = ASID_FIRST_VERSION;
  1147. local_flush_tlb_all(); /* start new asid cycle */
  1148. }
  1149. } while (smtc_live_asid[tlb][(asid & ASID_MASK)]);
  1150. /*
  1151. * SMTC shares the TLB within VPEs and possibly across all VPEs.
  1152. */
  1153. for_each_online_cpu(i) {
  1154. if ((smtc_status & SMTC_TLB_SHARED) ||
  1155. (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))
  1156. cpu_context(i, mm) = asid_cache(i) = asid;
  1157. }
  1158. if (smtc_status & SMTC_TLB_SHARED)
  1159. evpe(mtflags);
  1160. else
  1161. emt(mtflags);
  1162. local_irq_restore(flags);
  1163. }
  1164. /*
  1165. * Invoked from macros defined in mmu_context.h
  1166. * which must already have disabled interrupts
  1167. * and done a DVPE or DMT as appropriate.
  1168. */
  1169. void smtc_flush_tlb_asid(unsigned long asid)
  1170. {
  1171. int entry;
  1172. unsigned long ehi;
  1173. entry = read_c0_wired();
  1174. /* Traverse all non-wired entries */
  1175. while (entry < current_cpu_data.tlbsize) {
  1176. write_c0_index(entry);
  1177. ehb();
  1178. tlb_read();
  1179. ehb();
  1180. ehi = read_c0_entryhi();
  1181. if ((ehi & ASID_MASK) == asid) {
  1182. /*
  1183. * Invalidate only entries with specified ASID,
  1184. * makiing sure all entries differ.
  1185. */
  1186. write_c0_entryhi(CKSEG0 + (entry << (PAGE_SHIFT + 1)));
  1187. write_c0_entrylo0(0);
  1188. write_c0_entrylo1(0);
  1189. mtc0_tlbw_hazard();
  1190. tlb_write_indexed();
  1191. }
  1192. entry++;
  1193. }
  1194. write_c0_index(PARKED_INDEX);
  1195. tlbw_use_hazard();
  1196. }
  1197. /*
  1198. * Support for single-threading cache flush operations.
  1199. */
  1200. static int halt_state_save[NR_CPUS];
  1201. /*
  1202. * To really, really be sure that nothing is being done
  1203. * by other TCs, halt them all. This code assumes that
  1204. * a DVPE has already been done, so while their Halted
  1205. * state is theoretically architecturally unstable, in
  1206. * practice, it's not going to change while we're looking
  1207. * at it.
  1208. */
  1209. void smtc_cflush_lockdown(void)
  1210. {
  1211. int cpu;
  1212. for_each_online_cpu(cpu) {
  1213. if (cpu != smp_processor_id()) {
  1214. settc(cpu_data[cpu].tc_id);
  1215. halt_state_save[cpu] = read_tc_c0_tchalt();
  1216. write_tc_c0_tchalt(TCHALT_H);
  1217. }
  1218. }
  1219. mips_ihb();
  1220. }
  1221. /* It would be cheating to change the cpu_online states during a flush! */
  1222. void smtc_cflush_release(void)
  1223. {
  1224. int cpu;
  1225. /*
  1226. * Start with a hazard barrier to ensure
  1227. * that all CACHE ops have played through.
  1228. */
  1229. mips_ihb();
  1230. for_each_online_cpu(cpu) {
  1231. if (cpu != smp_processor_id()) {
  1232. settc(cpu_data[cpu].tc_id);
  1233. write_tc_c0_tchalt(halt_state_save[cpu]);
  1234. }
  1235. }
  1236. mips_ihb();
  1237. }