smtc.c 36 KB

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