smtc.c 37 KB

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