smtc.c 36 KB

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