smtc.c 33 KB

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