smtc.c 35 KB

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