smtc.c 37 KB

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