smtc.c 36 KB

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