smtc.c 36 KB

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