smtc.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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;
  79. /* Boot command line configuration overrides */
  80. static int vpe0limit;
  81. static int ipibuffers;
  82. static int nostlb;
  83. static int asidmask;
  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;
  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. if (tcpervpe[vpe] == 0)
  401. continue;
  402. if (vpe != 0)
  403. printk(", ");
  404. printk("VPE %d: TC", vpe);
  405. for (i = 0; i < tcpervpe[vpe]; i++) {
  406. /*
  407. * TC 0 is bound to VPE 0 at reset,
  408. * and is presumably executing this
  409. * code. Leave it alone!
  410. */
  411. if (tc != 0) {
  412. smtc_tc_setup(vpe, tc, cpu);
  413. cpu++;
  414. }
  415. printk(" %d", tc);
  416. tc++;
  417. }
  418. if (vpe != 0) {
  419. /*
  420. * Allow this VPE to control others.
  421. */
  422. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() |
  423. VPECONF0_MVP);
  424. /*
  425. * Clear any stale software interrupts from VPE's Cause
  426. */
  427. write_vpe_c0_cause(0);
  428. /*
  429. * Clear ERL/EXL of VPEs other than 0
  430. * and set restricted interrupt enable/mask.
  431. */
  432. write_vpe_c0_status((read_vpe_c0_status()
  433. & ~(ST0_BEV | ST0_ERL | ST0_EXL | ST0_IM))
  434. | (STATUSF_IP0 | STATUSF_IP1 | STATUSF_IP7
  435. | ST0_IE));
  436. /*
  437. * set config to be the same as vpe0,
  438. * particularly kseg0 coherency alg
  439. */
  440. write_vpe_c0_config(read_c0_config());
  441. /* Clear any pending timer interrupt */
  442. write_vpe_c0_compare(0);
  443. /* Propagate Config7 */
  444. write_vpe_c0_config7(read_c0_config7());
  445. write_vpe_c0_count(read_c0_count() + CP0_SKEW);
  446. ehb();
  447. }
  448. /* enable multi-threading within VPE */
  449. write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() | VPECONTROL_TE);
  450. /* enable the VPE */
  451. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA);
  452. }
  453. /*
  454. * Pull any physically present but unused TCs out of circulation.
  455. */
  456. while (tc < (((val & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1)) {
  457. cpu_clear(tc, cpu_possible_map);
  458. cpu_clear(tc, cpu_present_map);
  459. tc++;
  460. }
  461. /* release config state */
  462. write_c0_mvpcontrol( read_c0_mvpcontrol() & ~ MVPCONTROL_VPC );
  463. printk("\n");
  464. /* Set up coprocessor affinity CPU mask(s) */
  465. #ifdef CONFIG_MIPS_MT_FPAFF
  466. for (tc = 0; tc < ntc; tc++) {
  467. if (cpu_data[tc].options & MIPS_CPU_FPU)
  468. cpu_set(tc, mt_fpu_cpumask);
  469. }
  470. #endif
  471. /* set up ipi interrupts... */
  472. /* If we have multiple VPEs running, set up the cross-VPE interrupt */
  473. setup_cross_vpe_interrupts(nvpe);
  474. /* Set up queue of free IPI "messages". */
  475. nipi = NR_CPUS * IPIBUF_PER_CPU;
  476. if (ipibuffers > 0)
  477. nipi = ipibuffers;
  478. pipi = kmalloc(nipi *sizeof(struct smtc_ipi), GFP_KERNEL);
  479. if (pipi == NULL)
  480. panic("kmalloc of IPI message buffers failed\n");
  481. else
  482. printk("IPI buffer pool of %d buffers\n", nipi);
  483. for (i = 0; i < nipi; i++) {
  484. smtc_ipi_nq(&freeIPIq, pipi);
  485. pipi++;
  486. }
  487. /* Arm multithreading and enable other VPEs - but all TCs are Halted */
  488. emt(EMT_ENABLE);
  489. evpe(EVPE_ENABLE);
  490. local_irq_restore(flags);
  491. /* Initialize SMTC /proc statistics/diagnostics */
  492. init_smtc_stats();
  493. }
  494. /*
  495. * Setup the PC, SP, and GP of a secondary processor and start it
  496. * running!
  497. * smp_bootstrap is the place to resume from
  498. * __KSTK_TOS(idle) is apparently the stack pointer
  499. * (unsigned long)idle->thread_info the gp
  500. *
  501. */
  502. void __cpuinit smtc_boot_secondary(int cpu, struct task_struct *idle)
  503. {
  504. extern u32 kernelsp[NR_CPUS];
  505. unsigned long flags;
  506. int mtflags;
  507. LOCK_MT_PRA();
  508. if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
  509. dvpe();
  510. }
  511. settc(cpu_data[cpu].tc_id);
  512. /* pc */
  513. write_tc_c0_tcrestart((unsigned long)&smp_bootstrap);
  514. /* stack pointer */
  515. kernelsp[cpu] = __KSTK_TOS(idle);
  516. write_tc_gpr_sp(__KSTK_TOS(idle));
  517. /* global pointer */
  518. write_tc_gpr_gp((unsigned long)task_thread_info(idle));
  519. smtc_status |= SMTC_MTC_ACTIVE;
  520. write_tc_c0_tchalt(0);
  521. if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
  522. evpe(EVPE_ENABLE);
  523. }
  524. UNLOCK_MT_PRA();
  525. }
  526. void smtc_init_secondary(void)
  527. {
  528. local_irq_enable();
  529. }
  530. void smtc_smp_finish(void)
  531. {
  532. int cpu = smp_processor_id();
  533. /*
  534. * Lowest-numbered CPU per VPE starts a clock tick.
  535. * Like per_cpu_trap_init() hack, this assumes that
  536. * SMTC init code assigns TCs consdecutively and
  537. * in ascending order across available VPEs.
  538. */
  539. if (cpu > 0 && (cpu_data[cpu].vpe_id != cpu_data[cpu - 1].vpe_id))
  540. write_c0_compare(read_c0_count() + mips_hpt_frequency/HZ);
  541. printk("TC %d going on-line as CPU %d\n",
  542. cpu_data[smp_processor_id()].tc_id, smp_processor_id());
  543. }
  544. void smtc_cpus_done(void)
  545. {
  546. }
  547. /*
  548. * Support for SMTC-optimized driver IRQ registration
  549. */
  550. /*
  551. * SMTC Kernel needs to manipulate low-level CPU interrupt mask
  552. * in do_IRQ. These are passed in setup_irq_smtc() and stored
  553. * in this table.
  554. */
  555. int setup_irq_smtc(unsigned int irq, struct irqaction * new,
  556. unsigned long hwmask)
  557. {
  558. #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
  559. unsigned int vpe = current_cpu_data.vpe_id;
  560. vpemask[vpe][irq - MIPS_CPU_IRQ_BASE] = 1;
  561. #endif
  562. irq_hwmask[irq] = hwmask;
  563. return setup_irq(irq, new);
  564. }
  565. #ifdef CONFIG_MIPS_MT_SMTC_IRQAFF
  566. /*
  567. * Support for IRQ affinity to TCs
  568. */
  569. void smtc_set_irq_affinity(unsigned int irq, cpumask_t affinity)
  570. {
  571. /*
  572. * If a "fast path" cache of quickly decodable affinity state
  573. * is maintained, this is where it gets done, on a call up
  574. * from the platform affinity code.
  575. */
  576. }
  577. void smtc_forward_irq(unsigned int irq)
  578. {
  579. int target;
  580. /*
  581. * OK wise guy, now figure out how to get the IRQ
  582. * to be serviced on an authorized "CPU".
  583. *
  584. * Ideally, to handle the situation where an IRQ has multiple
  585. * eligible CPUS, we would maintain state per IRQ that would
  586. * allow a fair distribution of service requests. Since the
  587. * expected use model is any-or-only-one, for simplicity
  588. * and efficiency, we just pick the easiest one to find.
  589. */
  590. target = cpumask_first(irq_desc[irq].affinity);
  591. /*
  592. * We depend on the platform code to have correctly processed
  593. * IRQ affinity change requests to ensure that the IRQ affinity
  594. * mask has been purged of bits corresponding to nonexistent and
  595. * offline "CPUs", and to TCs bound to VPEs other than the VPE
  596. * connected to the physical interrupt input for the interrupt
  597. * in question. Otherwise we have a nasty problem with interrupt
  598. * mask management. This is best handled in non-performance-critical
  599. * platform IRQ affinity setting code, to minimize interrupt-time
  600. * checks.
  601. */
  602. /* If no one is eligible, service locally */
  603. if (target >= NR_CPUS) {
  604. do_IRQ_no_affinity(irq);
  605. return;
  606. }
  607. smtc_send_ipi(target, IRQ_AFFINITY_IPI, irq);
  608. }
  609. #endif /* CONFIG_MIPS_MT_SMTC_IRQAFF */
  610. /*
  611. * IPI model for SMTC is tricky, because interrupts aren't TC-specific.
  612. * Within a VPE one TC can interrupt another by different approaches.
  613. * The easiest to get right would probably be to make all TCs except
  614. * the target IXMT and set a software interrupt, but an IXMT-based
  615. * scheme requires that a handler must run before a new IPI could
  616. * be sent, which would break the "broadcast" loops in MIPS MT.
  617. * A more gonzo approach within a VPE is to halt the TC, extract
  618. * its Restart, Status, and a couple of GPRs, and program the Restart
  619. * address to emulate an interrupt.
  620. *
  621. * Within a VPE, one can be confident that the target TC isn't in
  622. * a critical EXL state when halted, since the write to the Halt
  623. * register could not have issued on the writing thread if the
  624. * halting thread had EXL set. So k0 and k1 of the target TC
  625. * can be used by the injection code. Across VPEs, one can't
  626. * be certain that the target TC isn't in a critical exception
  627. * state. So we try a two-step process of sending a software
  628. * interrupt to the target VPE, which either handles the event
  629. * itself (if it was the target) or injects the event within
  630. * the VPE.
  631. */
  632. static void smtc_ipi_qdump(void)
  633. {
  634. int i;
  635. for (i = 0; i < NR_CPUS ;i++) {
  636. printk("IPIQ[%d]: head = 0x%x, tail = 0x%x, depth = %d\n",
  637. i, (unsigned)IPIQ[i].head, (unsigned)IPIQ[i].tail,
  638. IPIQ[i].depth);
  639. }
  640. }
  641. /*
  642. * The standard atomic.h primitives don't quite do what we want
  643. * here: We need an atomic add-and-return-previous-value (which
  644. * could be done with atomic_add_return and a decrement) and an
  645. * atomic set/zero-and-return-previous-value (which can't really
  646. * be done with the atomic.h primitives). And since this is
  647. * MIPS MT, we can assume that we have LL/SC.
  648. */
  649. static inline int atomic_postincrement(atomic_t *v)
  650. {
  651. unsigned long result;
  652. unsigned long temp;
  653. __asm__ __volatile__(
  654. "1: ll %0, %2 \n"
  655. " addu %1, %0, 1 \n"
  656. " sc %1, %2 \n"
  657. " beqz %1, 1b \n"
  658. __WEAK_LLSC_MB
  659. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  660. : "m" (v->counter)
  661. : "memory");
  662. return result;
  663. }
  664. void smtc_send_ipi(int cpu, int type, unsigned int action)
  665. {
  666. int tcstatus;
  667. struct smtc_ipi *pipi;
  668. unsigned long flags;
  669. int mtflags;
  670. unsigned long tcrestart;
  671. extern void r4k_wait_irqoff(void), __pastwait(void);
  672. if (cpu == smp_processor_id()) {
  673. printk("Cannot Send IPI to self!\n");
  674. return;
  675. }
  676. /* Set up a descriptor, to be delivered either promptly or queued */
  677. pipi = smtc_ipi_dq(&freeIPIq);
  678. if (pipi == NULL) {
  679. bust_spinlocks(1);
  680. mips_mt_regdump(dvpe());
  681. panic("IPI Msg. Buffers Depleted\n");
  682. }
  683. pipi->type = type;
  684. pipi->arg = (void *)action;
  685. pipi->dest = cpu;
  686. if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
  687. /* If not on same VPE, enqueue and send cross-VPE interrupt */
  688. smtc_ipi_nq(&IPIQ[cpu], pipi);
  689. LOCK_CORE_PRA();
  690. settc(cpu_data[cpu].tc_id);
  691. write_vpe_c0_cause(read_vpe_c0_cause() | C_SW1);
  692. UNLOCK_CORE_PRA();
  693. } else {
  694. /*
  695. * Not sufficient to do a LOCK_MT_PRA (dmt) here,
  696. * since ASID shootdown on the other VPE may
  697. * collide with this operation.
  698. */
  699. LOCK_CORE_PRA();
  700. settc(cpu_data[cpu].tc_id);
  701. /* Halt the targeted TC */
  702. write_tc_c0_tchalt(TCHALT_H);
  703. mips_ihb();
  704. /*
  705. * Inspect TCStatus - if IXMT is set, we have to queue
  706. * a message. Otherwise, we set up the "interrupt"
  707. * of the other TC
  708. */
  709. tcstatus = read_tc_c0_tcstatus();
  710. if ((tcstatus & TCSTATUS_IXMT) != 0) {
  711. /*
  712. * If we're in the the irq-off version of the wait
  713. * loop, we need to force exit from the wait and
  714. * do a direct post of the IPI.
  715. */
  716. if (cpu_wait == r4k_wait_irqoff) {
  717. tcrestart = read_tc_c0_tcrestart();
  718. if (tcrestart >= (unsigned long)r4k_wait_irqoff
  719. && tcrestart < (unsigned long)__pastwait) {
  720. write_tc_c0_tcrestart(__pastwait);
  721. tcstatus &= ~TCSTATUS_IXMT;
  722. write_tc_c0_tcstatus(tcstatus);
  723. goto postdirect;
  724. }
  725. }
  726. /*
  727. * Otherwise we queue the message for the target TC
  728. * to pick up when he does a local_irq_restore()
  729. */
  730. write_tc_c0_tchalt(0);
  731. UNLOCK_CORE_PRA();
  732. smtc_ipi_nq(&IPIQ[cpu], pipi);
  733. } else {
  734. postdirect:
  735. post_direct_ipi(cpu, pipi);
  736. write_tc_c0_tchalt(0);
  737. UNLOCK_CORE_PRA();
  738. }
  739. }
  740. }
  741. /*
  742. * Send IPI message to Halted TC, TargTC/TargVPE already having been set
  743. */
  744. static void post_direct_ipi(int cpu, struct smtc_ipi *pipi)
  745. {
  746. struct pt_regs *kstack;
  747. unsigned long tcstatus;
  748. unsigned long tcrestart;
  749. extern u32 kernelsp[NR_CPUS];
  750. extern void __smtc_ipi_vector(void);
  751. //printk("%s: on %d for %d\n", __func__, smp_processor_id(), cpu);
  752. /* Extract Status, EPC from halted TC */
  753. tcstatus = read_tc_c0_tcstatus();
  754. tcrestart = read_tc_c0_tcrestart();
  755. /* If TCRestart indicates a WAIT instruction, advance the PC */
  756. if ((tcrestart & 0x80000000)
  757. && ((*(unsigned int *)tcrestart & 0xfe00003f) == 0x42000020)) {
  758. tcrestart += 4;
  759. }
  760. /*
  761. * Save on TC's future kernel stack
  762. *
  763. * CU bit of Status is indicator that TC was
  764. * already running on a kernel stack...
  765. */
  766. if (tcstatus & ST0_CU0) {
  767. /* Note that this "- 1" is pointer arithmetic */
  768. kstack = ((struct pt_regs *)read_tc_gpr_sp()) - 1;
  769. } else {
  770. kstack = ((struct pt_regs *)kernelsp[cpu]) - 1;
  771. }
  772. kstack->cp0_epc = (long)tcrestart;
  773. /* Save TCStatus */
  774. kstack->cp0_tcstatus = tcstatus;
  775. /* Pass token of operation to be performed kernel stack pad area */
  776. kstack->pad0[4] = (unsigned long)pipi;
  777. /* Pass address of function to be called likewise */
  778. kstack->pad0[5] = (unsigned long)&ipi_decode;
  779. /* Set interrupt exempt and kernel mode */
  780. tcstatus |= TCSTATUS_IXMT;
  781. tcstatus &= ~TCSTATUS_TKSU;
  782. write_tc_c0_tcstatus(tcstatus);
  783. ehb();
  784. /* Set TC Restart address to be SMTC IPI vector */
  785. write_tc_c0_tcrestart(__smtc_ipi_vector);
  786. }
  787. static void ipi_resched_interrupt(void)
  788. {
  789. /* Return from interrupt should be enough to cause scheduler check */
  790. }
  791. static void ipi_call_interrupt(void)
  792. {
  793. /* Invoke generic function invocation code in smp.c */
  794. smp_call_function_interrupt();
  795. }
  796. DECLARE_PER_CPU(struct clock_event_device, mips_clockevent_device);
  797. void ipi_decode(struct smtc_ipi *pipi)
  798. {
  799. unsigned int cpu = smp_processor_id();
  800. struct clock_event_device *cd;
  801. void *arg_copy = pipi->arg;
  802. int type_copy = pipi->type;
  803. int irq = MIPS_CPU_IRQ_BASE + 1;
  804. smtc_ipi_nq(&freeIPIq, pipi);
  805. switch (type_copy) {
  806. case SMTC_CLOCK_TICK:
  807. irq_enter();
  808. kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq));
  809. cd = &per_cpu(mips_clockevent_device, cpu);
  810. cd->event_handler(cd);
  811. irq_exit();
  812. break;
  813. case LINUX_SMP_IPI:
  814. switch ((int)arg_copy) {
  815. case SMP_RESCHEDULE_YOURSELF:
  816. ipi_resched_interrupt();
  817. break;
  818. case SMP_CALL_FUNCTION:
  819. ipi_call_interrupt();
  820. break;
  821. default:
  822. printk("Impossible SMTC IPI Argument 0x%x\n",
  823. (int)arg_copy);
  824. break;
  825. }
  826. break;
  827. #ifdef CONFIG_MIPS_MT_SMTC_IRQAFF
  828. case IRQ_AFFINITY_IPI:
  829. /*
  830. * Accept a "forwarded" interrupt that was initially
  831. * taken by a TC who doesn't have affinity for the IRQ.
  832. */
  833. do_IRQ_no_affinity((int)arg_copy);
  834. break;
  835. #endif /* CONFIG_MIPS_MT_SMTC_IRQAFF */
  836. default:
  837. printk("Impossible SMTC IPI Type 0x%x\n", type_copy);
  838. break;
  839. }
  840. }
  841. /*
  842. * Similar to smtc_ipi_replay(), but invoked from context restore,
  843. * so it reuses the current exception frame rather than set up a
  844. * new one with self_ipi.
  845. */
  846. void deferred_smtc_ipi(void)
  847. {
  848. int cpu = smp_processor_id();
  849. /*
  850. * Test is not atomic, but much faster than a dequeue,
  851. * and the vast majority of invocations will have a null queue.
  852. * If irq_disabled when this was called, then any IPIs queued
  853. * after we test last will be taken on the next irq_enable/restore.
  854. * If interrupts were enabled, then any IPIs added after the
  855. * last test will be taken directly.
  856. */
  857. while (IPIQ[cpu].head != NULL) {
  858. struct smtc_ipi_q *q = &IPIQ[cpu];
  859. struct smtc_ipi *pipi;
  860. unsigned long flags;
  861. /*
  862. * It may be possible we'll come in with interrupts
  863. * already enabled.
  864. */
  865. local_irq_save(flags);
  866. spin_lock(&q->lock);
  867. pipi = __smtc_ipi_dq(q);
  868. spin_unlock(&q->lock);
  869. if (pipi != NULL)
  870. ipi_decode(pipi);
  871. /*
  872. * The use of the __raw_local restore isn't
  873. * as obviously necessary here as in smtc_ipi_replay(),
  874. * but it's more efficient, given that we're already
  875. * running down the IPI queue.
  876. */
  877. __raw_local_irq_restore(flags);
  878. }
  879. }
  880. /*
  881. * Cross-VPE interrupts in the SMTC prototype use "software interrupts"
  882. * set via cross-VPE MTTR manipulation of the Cause register. It would be
  883. * in some regards preferable to have external logic for "doorbell" hardware
  884. * interrupts.
  885. */
  886. static int cpu_ipi_irq = MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_IRQ;
  887. static irqreturn_t ipi_interrupt(int irq, void *dev_idm)
  888. {
  889. int my_vpe = cpu_data[smp_processor_id()].vpe_id;
  890. int my_tc = cpu_data[smp_processor_id()].tc_id;
  891. int cpu;
  892. struct smtc_ipi *pipi;
  893. unsigned long tcstatus;
  894. int sent;
  895. unsigned long flags;
  896. unsigned int mtflags;
  897. unsigned int vpflags;
  898. /*
  899. * So long as cross-VPE interrupts are done via
  900. * MFTR/MTTR read-modify-writes of Cause, we need
  901. * to stop other VPEs whenever the local VPE does
  902. * anything similar.
  903. */
  904. local_irq_save(flags);
  905. vpflags = dvpe();
  906. clear_c0_cause(0x100 << MIPS_CPU_IPI_IRQ);
  907. set_c0_status(0x100 << MIPS_CPU_IPI_IRQ);
  908. irq_enable_hazard();
  909. evpe(vpflags);
  910. local_irq_restore(flags);
  911. /*
  912. * Cross-VPE Interrupt handler: Try to directly deliver IPIs
  913. * queued for TCs on this VPE other than the current one.
  914. * Return-from-interrupt should cause us to drain the queue
  915. * for the current TC, so we ought not to have to do it explicitly here.
  916. */
  917. for_each_online_cpu(cpu) {
  918. if (cpu_data[cpu].vpe_id != my_vpe)
  919. continue;
  920. pipi = smtc_ipi_dq(&IPIQ[cpu]);
  921. if (pipi != NULL) {
  922. if (cpu_data[cpu].tc_id != my_tc) {
  923. sent = 0;
  924. LOCK_MT_PRA();
  925. settc(cpu_data[cpu].tc_id);
  926. write_tc_c0_tchalt(TCHALT_H);
  927. mips_ihb();
  928. tcstatus = read_tc_c0_tcstatus();
  929. if ((tcstatus & TCSTATUS_IXMT) == 0) {
  930. post_direct_ipi(cpu, pipi);
  931. sent = 1;
  932. }
  933. write_tc_c0_tchalt(0);
  934. UNLOCK_MT_PRA();
  935. if (!sent) {
  936. smtc_ipi_req(&IPIQ[cpu], pipi);
  937. }
  938. } else {
  939. /*
  940. * ipi_decode() should be called
  941. * with interrupts off
  942. */
  943. local_irq_save(flags);
  944. ipi_decode(pipi);
  945. local_irq_restore(flags);
  946. }
  947. }
  948. }
  949. return IRQ_HANDLED;
  950. }
  951. static void ipi_irq_dispatch(void)
  952. {
  953. do_IRQ(cpu_ipi_irq);
  954. }
  955. static struct irqaction irq_ipi = {
  956. .handler = ipi_interrupt,
  957. .flags = IRQF_DISABLED,
  958. .name = "SMTC_IPI",
  959. .flags = IRQF_PERCPU
  960. };
  961. static void setup_cross_vpe_interrupts(unsigned int nvpe)
  962. {
  963. if (nvpe < 1)
  964. return;
  965. if (!cpu_has_vint)
  966. panic("SMTC Kernel requires Vectored Interrupt support");
  967. set_vi_handler(MIPS_CPU_IPI_IRQ, ipi_irq_dispatch);
  968. setup_irq_smtc(cpu_ipi_irq, &irq_ipi, (0x100 << MIPS_CPU_IPI_IRQ));
  969. set_irq_handler(cpu_ipi_irq, handle_percpu_irq);
  970. }
  971. /*
  972. * SMTC-specific hacks invoked from elsewhere in the kernel.
  973. */
  974. /*
  975. * smtc_ipi_replay is called from raw_local_irq_restore
  976. */
  977. void smtc_ipi_replay(void)
  978. {
  979. unsigned int cpu = smp_processor_id();
  980. /*
  981. * To the extent that we've ever turned interrupts off,
  982. * we may have accumulated deferred IPIs. This is subtle.
  983. * we should be OK: If we pick up something and dispatch
  984. * it here, that's great. If we see nothing, but concurrent
  985. * with this operation, another TC sends us an IPI, IXMT
  986. * is clear, and we'll handle it as a real pseudo-interrupt
  987. * and not a pseudo-pseudo interrupt. The important thing
  988. * is to do the last check for queued message *after* the
  989. * re-enabling of interrupts.
  990. */
  991. while (IPIQ[cpu].head != NULL) {
  992. struct smtc_ipi_q *q = &IPIQ[cpu];
  993. struct smtc_ipi *pipi;
  994. unsigned long flags;
  995. /*
  996. * It's just possible we'll come in with interrupts
  997. * already enabled.
  998. */
  999. local_irq_save(flags);
  1000. spin_lock(&q->lock);
  1001. pipi = __smtc_ipi_dq(q);
  1002. spin_unlock(&q->lock);
  1003. /*
  1004. ** But use a raw restore here to avoid recursion.
  1005. */
  1006. __raw_local_irq_restore(flags);
  1007. if (pipi) {
  1008. self_ipi(pipi);
  1009. smtc_cpu_stats[cpu].selfipis++;
  1010. }
  1011. }
  1012. }
  1013. EXPORT_SYMBOL(smtc_ipi_replay);
  1014. void smtc_idle_loop_hook(void)
  1015. {
  1016. #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
  1017. int im;
  1018. int flags;
  1019. int mtflags;
  1020. int bit;
  1021. int vpe;
  1022. int tc;
  1023. int hook_ntcs;
  1024. /*
  1025. * printk within DMT-protected regions can deadlock,
  1026. * so buffer diagnostic messages for later output.
  1027. */
  1028. char *pdb_msg;
  1029. char id_ho_db_msg[768]; /* worst-case use should be less than 700 */
  1030. if (atomic_read(&idle_hook_initialized) == 0) { /* fast test */
  1031. if (atomic_add_return(1, &idle_hook_initialized) == 1) {
  1032. int mvpconf0;
  1033. /* Tedious stuff to just do once */
  1034. mvpconf0 = read_c0_mvpconf0();
  1035. hook_ntcs = ((mvpconf0 & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
  1036. if (hook_ntcs > NR_CPUS)
  1037. hook_ntcs = NR_CPUS;
  1038. for (tc = 0; tc < hook_ntcs; tc++) {
  1039. tcnoprog[tc] = 0;
  1040. clock_hang_reported[tc] = 0;
  1041. }
  1042. for (vpe = 0; vpe < 2; vpe++)
  1043. for (im = 0; im < 8; im++)
  1044. imstuckcount[vpe][im] = 0;
  1045. printk("Idle loop test hook initialized for %d TCs\n", hook_ntcs);
  1046. atomic_set(&idle_hook_initialized, 1000);
  1047. } else {
  1048. /* Someone else is initializing in parallel - let 'em finish */
  1049. while (atomic_read(&idle_hook_initialized) < 1000)
  1050. ;
  1051. }
  1052. }
  1053. /* Have we stupidly left IXMT set somewhere? */
  1054. if (read_c0_tcstatus() & 0x400) {
  1055. write_c0_tcstatus(read_c0_tcstatus() & ~0x400);
  1056. ehb();
  1057. printk("Dangling IXMT in cpu_idle()\n");
  1058. }
  1059. /* Have we stupidly left an IM bit turned off? */
  1060. #define IM_LIMIT 2000
  1061. local_irq_save(flags);
  1062. mtflags = dmt();
  1063. pdb_msg = &id_ho_db_msg[0];
  1064. im = read_c0_status();
  1065. vpe = current_cpu_data.vpe_id;
  1066. for (bit = 0; bit < 8; bit++) {
  1067. /*
  1068. * In current prototype, I/O interrupts
  1069. * are masked for VPE > 0
  1070. */
  1071. if (vpemask[vpe][bit]) {
  1072. if (!(im & (0x100 << bit)))
  1073. imstuckcount[vpe][bit]++;
  1074. else
  1075. imstuckcount[vpe][bit] = 0;
  1076. if (imstuckcount[vpe][bit] > IM_LIMIT) {
  1077. set_c0_status(0x100 << bit);
  1078. ehb();
  1079. imstuckcount[vpe][bit] = 0;
  1080. pdb_msg += sprintf(pdb_msg,
  1081. "Dangling IM %d fixed for VPE %d\n", bit,
  1082. vpe);
  1083. }
  1084. }
  1085. }
  1086. emt(mtflags);
  1087. local_irq_restore(flags);
  1088. if (pdb_msg != &id_ho_db_msg[0])
  1089. printk("CPU%d: %s", smp_processor_id(), id_ho_db_msg);
  1090. #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
  1091. smtc_ipi_replay();
  1092. }
  1093. void smtc_soft_dump(void)
  1094. {
  1095. int i;
  1096. printk("Counter Interrupts taken per CPU (TC)\n");
  1097. for (i=0; i < NR_CPUS; i++) {
  1098. printk("%d: %ld\n", i, smtc_cpu_stats[i].timerints);
  1099. }
  1100. printk("Self-IPI invocations:\n");
  1101. for (i=0; i < NR_CPUS; i++) {
  1102. printk("%d: %ld\n", i, smtc_cpu_stats[i].selfipis);
  1103. }
  1104. smtc_ipi_qdump();
  1105. printk("%d Recoveries of \"stolen\" FPU\n",
  1106. atomic_read(&smtc_fpu_recoveries));
  1107. }
  1108. /*
  1109. * TLB management routines special to SMTC
  1110. */
  1111. void smtc_get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
  1112. {
  1113. unsigned long flags, mtflags, tcstat, prevhalt, asid;
  1114. int tlb, i;
  1115. /*
  1116. * It would be nice to be able to use a spinlock here,
  1117. * but this is invoked from within TLB flush routines
  1118. * that protect themselves with DVPE, so if a lock is
  1119. * held by another TC, it'll never be freed.
  1120. *
  1121. * DVPE/DMT must not be done with interrupts enabled,
  1122. * so even so most callers will already have disabled
  1123. * them, let's be really careful...
  1124. */
  1125. local_irq_save(flags);
  1126. if (smtc_status & SMTC_TLB_SHARED) {
  1127. mtflags = dvpe();
  1128. tlb = 0;
  1129. } else {
  1130. mtflags = dmt();
  1131. tlb = cpu_data[cpu].vpe_id;
  1132. }
  1133. asid = asid_cache(cpu);
  1134. do {
  1135. if (!((asid += ASID_INC) & ASID_MASK) ) {
  1136. if (cpu_has_vtag_icache)
  1137. flush_icache_all();
  1138. /* Traverse all online CPUs (hack requires contigous range) */
  1139. for_each_online_cpu(i) {
  1140. /*
  1141. * We don't need to worry about our own CPU, nor those of
  1142. * CPUs who don't share our TLB.
  1143. */
  1144. if ((i != smp_processor_id()) &&
  1145. ((smtc_status & SMTC_TLB_SHARED) ||
  1146. (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))) {
  1147. settc(cpu_data[i].tc_id);
  1148. prevhalt = read_tc_c0_tchalt() & TCHALT_H;
  1149. if (!prevhalt) {
  1150. write_tc_c0_tchalt(TCHALT_H);
  1151. mips_ihb();
  1152. }
  1153. tcstat = read_tc_c0_tcstatus();
  1154. smtc_live_asid[tlb][(tcstat & ASID_MASK)] |= (asiduse)(0x1 << i);
  1155. if (!prevhalt)
  1156. write_tc_c0_tchalt(0);
  1157. }
  1158. }
  1159. if (!asid) /* fix version if needed */
  1160. asid = ASID_FIRST_VERSION;
  1161. local_flush_tlb_all(); /* start new asid cycle */
  1162. }
  1163. } while (smtc_live_asid[tlb][(asid & ASID_MASK)]);
  1164. /*
  1165. * SMTC shares the TLB within VPEs and possibly across all VPEs.
  1166. */
  1167. for_each_online_cpu(i) {
  1168. if ((smtc_status & SMTC_TLB_SHARED) ||
  1169. (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))
  1170. cpu_context(i, mm) = asid_cache(i) = asid;
  1171. }
  1172. if (smtc_status & SMTC_TLB_SHARED)
  1173. evpe(mtflags);
  1174. else
  1175. emt(mtflags);
  1176. local_irq_restore(flags);
  1177. }
  1178. /*
  1179. * Invoked from macros defined in mmu_context.h
  1180. * which must already have disabled interrupts
  1181. * and done a DVPE or DMT as appropriate.
  1182. */
  1183. void smtc_flush_tlb_asid(unsigned long asid)
  1184. {
  1185. int entry;
  1186. unsigned long ehi;
  1187. entry = read_c0_wired();
  1188. /* Traverse all non-wired entries */
  1189. while (entry < current_cpu_data.tlbsize) {
  1190. write_c0_index(entry);
  1191. ehb();
  1192. tlb_read();
  1193. ehb();
  1194. ehi = read_c0_entryhi();
  1195. if ((ehi & ASID_MASK) == asid) {
  1196. /*
  1197. * Invalidate only entries with specified ASID,
  1198. * makiing sure all entries differ.
  1199. */
  1200. write_c0_entryhi(CKSEG0 + (entry << (PAGE_SHIFT + 1)));
  1201. write_c0_entrylo0(0);
  1202. write_c0_entrylo1(0);
  1203. mtc0_tlbw_hazard();
  1204. tlb_write_indexed();
  1205. }
  1206. entry++;
  1207. }
  1208. write_c0_index(PARKED_INDEX);
  1209. tlbw_use_hazard();
  1210. }
  1211. /*
  1212. * Support for single-threading cache flush operations.
  1213. */
  1214. static int halt_state_save[NR_CPUS];
  1215. /*
  1216. * To really, really be sure that nothing is being done
  1217. * by other TCs, halt them all. This code assumes that
  1218. * a DVPE has already been done, so while their Halted
  1219. * state is theoretically architecturally unstable, in
  1220. * practice, it's not going to change while we're looking
  1221. * at it.
  1222. */
  1223. void smtc_cflush_lockdown(void)
  1224. {
  1225. int cpu;
  1226. for_each_online_cpu(cpu) {
  1227. if (cpu != smp_processor_id()) {
  1228. settc(cpu_data[cpu].tc_id);
  1229. halt_state_save[cpu] = read_tc_c0_tchalt();
  1230. write_tc_c0_tchalt(TCHALT_H);
  1231. }
  1232. }
  1233. mips_ihb();
  1234. }
  1235. /* It would be cheating to change the cpu_online states during a flush! */
  1236. void smtc_cflush_release(void)
  1237. {
  1238. int cpu;
  1239. /*
  1240. * Start with a hazard barrier to ensure
  1241. * that all CACHE ops have played through.
  1242. */
  1243. mips_ihb();
  1244. for_each_online_cpu(cpu) {
  1245. if (cpu != smp_processor_id()) {
  1246. settc(cpu_data[cpu].tc_id);
  1247. write_tc_c0_tchalt(halt_state_save[cpu]);
  1248. }
  1249. }
  1250. mips_ihb();
  1251. }