smtc.c 37 KB

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