smtc.c 36 KB

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