smtc.c 37 KB

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