smtc.c 36 KB

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