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 <linux/slab.h>
  29. #include <asm/cpu.h>
  30. #include <asm/processor.h>
  31. #include <asm/atomic.h>
  32. #include <asm/system.h>
  33. #include <asm/hardirq.h>
  34. #include <asm/hazards.h>
  35. #include <asm/irq.h>
  36. #include <asm/mmu_context.h>
  37. #include <asm/mipsregs.h>
  38. #include <asm/cacheflush.h>
  39. #include <asm/time.h>
  40. #include <asm/addrspace.h>
  41. #include <asm/smtc.h>
  42. #include <asm/smtc_proc.h>
  43. /*
  44. * SMTC Kernel needs to manipulate low-level CPU interrupt mask
  45. * in do_IRQ. These are passed in setup_irq_smtc() and stored
  46. * in this table.
  47. */
  48. unsigned long irq_hwmask[NR_IRQS];
  49. #define LOCK_MT_PRA() \
  50. local_irq_save(flags); \
  51. mtflags = dmt()
  52. #define UNLOCK_MT_PRA() \
  53. emt(mtflags); \
  54. local_irq_restore(flags)
  55. #define LOCK_CORE_PRA() \
  56. local_irq_save(flags); \
  57. mtflags = dvpe()
  58. #define UNLOCK_CORE_PRA() \
  59. evpe(mtflags); \
  60. local_irq_restore(flags)
  61. /*
  62. * Data structures purely associated with SMTC parallelism
  63. */
  64. /*
  65. * Table for tracking ASIDs whose lifetime is prolonged.
  66. */
  67. asiduse smtc_live_asid[MAX_SMTC_TLBS][MAX_SMTC_ASIDS];
  68. /*
  69. * Number of InterProcessor Interrupt (IPI) message buffers to allocate
  70. */
  71. #define IPIBUF_PER_CPU 4
  72. struct smtc_ipi_q IPIQ[NR_CPUS];
  73. static struct smtc_ipi_q freeIPIq;
  74. /* Forward declarations */
  75. void ipi_decode(struct smtc_ipi *);
  76. static void post_direct_ipi(int cpu, struct smtc_ipi *pipi);
  77. static void setup_cross_vpe_interrupts(unsigned int nvpe);
  78. void init_smtc_stats(void);
  79. /* Global SMTC Status */
  80. unsigned int smtc_status;
  81. /* Boot command line configuration overrides */
  82. static int vpe0limit;
  83. static int ipibuffers;
  84. static int nostlb;
  85. static int asidmask;
  86. unsigned long smtc_asid_mask = 0xff;
  87. static int __init vpe0tcs(char *str)
  88. {
  89. get_option(&str, &vpe0limit);
  90. return 1;
  91. }
  92. static int __init ipibufs(char *str)
  93. {
  94. get_option(&str, &ipibuffers);
  95. return 1;
  96. }
  97. static int __init stlb_disable(char *s)
  98. {
  99. nostlb = 1;
  100. return 1;
  101. }
  102. static int __init asidmask_set(char *str)
  103. {
  104. get_option(&str, &asidmask);
  105. switch (asidmask) {
  106. case 0x1:
  107. case 0x3:
  108. case 0x7:
  109. case 0xf:
  110. case 0x1f:
  111. case 0x3f:
  112. case 0x7f:
  113. case 0xff:
  114. smtc_asid_mask = (unsigned long)asidmask;
  115. break;
  116. default:
  117. printk("ILLEGAL ASID mask 0x%x from command line\n", asidmask);
  118. }
  119. return 1;
  120. }
  121. __setup("vpe0tcs=", vpe0tcs);
  122. __setup("ipibufs=", ipibufs);
  123. __setup("nostlb", stlb_disable);
  124. __setup("asidmask=", asidmask_set);
  125. #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
  126. static int hang_trig;
  127. static int __init hangtrig_enable(char *s)
  128. {
  129. hang_trig = 1;
  130. return 1;
  131. }
  132. __setup("hangtrig", hangtrig_enable);
  133. #define DEFAULT_BLOCKED_IPI_LIMIT 32
  134. static int timerq_limit = DEFAULT_BLOCKED_IPI_LIMIT;
  135. static int __init tintq(char *str)
  136. {
  137. get_option(&str, &timerq_limit);
  138. return 1;
  139. }
  140. __setup("tintq=", tintq);
  141. static int imstuckcount[2][8];
  142. /* vpemask represents IM/IE bits of per-VPE Status registers, low-to-high */
  143. static int vpemask[2][8] = {
  144. {0, 0, 1, 0, 0, 0, 0, 1},
  145. {0, 0, 0, 0, 0, 0, 0, 1}
  146. };
  147. int tcnoprog[NR_CPUS];
  148. static atomic_t idle_hook_initialized = ATOMIC_INIT(0);
  149. static int clock_hang_reported[NR_CPUS];
  150. #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
  151. /*
  152. * Configure shared TLB - VPC configuration bit must be set by caller
  153. */
  154. static void smtc_configure_tlb(void)
  155. {
  156. int i, tlbsiz, vpes;
  157. unsigned long mvpconf0;
  158. unsigned long config1val;
  159. /* Set up ASID preservation table */
  160. for (vpes=0; vpes<MAX_SMTC_TLBS; vpes++) {
  161. for(i = 0; i < MAX_SMTC_ASIDS; i++) {
  162. smtc_live_asid[vpes][i] = 0;
  163. }
  164. }
  165. mvpconf0 = read_c0_mvpconf0();
  166. if ((vpes = ((mvpconf0 & MVPCONF0_PVPE)
  167. >> MVPCONF0_PVPE_SHIFT) + 1) > 1) {
  168. /* If we have multiple VPEs, try to share the TLB */
  169. if ((mvpconf0 & MVPCONF0_TLBS) && !nostlb) {
  170. /*
  171. * If TLB sizing is programmable, shared TLB
  172. * size is the total available complement.
  173. * Otherwise, we have to take the sum of all
  174. * static VPE TLB entries.
  175. */
  176. if ((tlbsiz = ((mvpconf0 & MVPCONF0_PTLBE)
  177. >> MVPCONF0_PTLBE_SHIFT)) == 0) {
  178. /*
  179. * If there's more than one VPE, there had better
  180. * be more than one TC, because we need one to bind
  181. * to each VPE in turn to be able to read
  182. * its configuration state!
  183. */
  184. settc(1);
  185. /* Stop the TC from doing anything foolish */
  186. write_tc_c0_tchalt(TCHALT_H);
  187. mips_ihb();
  188. /* No need to un-Halt - that happens later anyway */
  189. for (i=0; i < vpes; i++) {
  190. write_tc_c0_tcbind(i);
  191. /*
  192. * To be 100% sure we're really getting the right
  193. * information, we exit the configuration state
  194. * and do an IHB after each rebinding.
  195. */
  196. write_c0_mvpcontrol(
  197. read_c0_mvpcontrol() & ~ MVPCONTROL_VPC );
  198. mips_ihb();
  199. /*
  200. * Only count if the MMU Type indicated is TLB
  201. */
  202. if (((read_vpe_c0_config() & MIPS_CONF_MT) >> 7) == 1) {
  203. config1val = read_vpe_c0_config1();
  204. tlbsiz += ((config1val >> 25) & 0x3f) + 1;
  205. }
  206. /* Put core back in configuration state */
  207. write_c0_mvpcontrol(
  208. read_c0_mvpcontrol() | MVPCONTROL_VPC );
  209. mips_ihb();
  210. }
  211. }
  212. write_c0_mvpcontrol(read_c0_mvpcontrol() | MVPCONTROL_STLB);
  213. ehb();
  214. /*
  215. * Setup kernel data structures to use software total,
  216. * rather than read the per-VPE Config1 value. The values
  217. * for "CPU 0" gets copied to all the other CPUs as part
  218. * of their initialization in smtc_cpu_setup().
  219. */
  220. /* MIPS32 limits TLB indices to 64 */
  221. if (tlbsiz > 64)
  222. tlbsiz = 64;
  223. cpu_data[0].tlbsize = current_cpu_data.tlbsize = tlbsiz;
  224. smtc_status |= SMTC_TLB_SHARED;
  225. local_flush_tlb_all();
  226. printk("TLB of %d entry pairs shared by %d VPEs\n",
  227. tlbsiz, vpes);
  228. } else {
  229. printk("WARNING: TLB Not Sharable on SMTC Boot!\n");
  230. }
  231. }
  232. }
  233. /*
  234. * Incrementally build the CPU map out of constituent MIPS MT cores,
  235. * using the specified available VPEs and TCs. Plaform code needs
  236. * to ensure that each MIPS MT core invokes this routine on reset,
  237. * one at a time(!).
  238. *
  239. * This version of the build_cpu_map and prepare_cpus routines assumes
  240. * that *all* TCs of a MIPS MT core will be used for Linux, and that
  241. * they will be spread across *all* available VPEs (to minimise the
  242. * loss of efficiency due to exception service serialization).
  243. * An improved version would pick up configuration information and
  244. * possibly leave some TCs/VPEs as "slave" processors.
  245. *
  246. * Use c0_MVPConf0 to find out how many TCs are available, setting up
  247. * cpu_possible_map and the logical/physical mappings.
  248. */
  249. int __init smtc_build_cpu_map(int start_cpu_slot)
  250. {
  251. int i, ntcs;
  252. /*
  253. * The CPU map isn't actually used for anything at this point,
  254. * so it's not clear what else we should do apart from set
  255. * everything up so that "logical" = "physical".
  256. */
  257. ntcs = ((read_c0_mvpconf0() & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
  258. for (i=start_cpu_slot; i<NR_CPUS && i<ntcs; i++) {
  259. set_cpu_possible(i, true);
  260. __cpu_number_map[i] = i;
  261. __cpu_logical_map[i] = i;
  262. }
  263. #ifdef CONFIG_MIPS_MT_FPAFF
  264. /* Initialize map of CPUs with FPUs */
  265. cpus_clear(mt_fpu_cpumask);
  266. #endif
  267. /* One of those TC's is the one booting, and not a secondary... */
  268. printk("%i available secondary CPU TC(s)\n", i - 1);
  269. return i;
  270. }
  271. /*
  272. * Common setup before any secondaries are started
  273. * Make sure all CPU's are in a sensible state before we boot any of the
  274. * secondaries.
  275. *
  276. * For MIPS MT "SMTC" operation, we set up all TCs, spread as evenly
  277. * as possible across the available VPEs.
  278. */
  279. static void smtc_tc_setup(int vpe, int tc, int cpu)
  280. {
  281. settc(tc);
  282. write_tc_c0_tchalt(TCHALT_H);
  283. mips_ihb();
  284. write_tc_c0_tcstatus((read_tc_c0_tcstatus()
  285. & ~(TCSTATUS_TKSU | TCSTATUS_DA | TCSTATUS_IXMT))
  286. | TCSTATUS_A);
  287. /*
  288. * TCContext gets an offset from the base of the IPIQ array
  289. * to be used in low-level code to detect the presence of
  290. * an active IPI queue
  291. */
  292. write_tc_c0_tccontext((sizeof(struct smtc_ipi_q) * cpu) << 16);
  293. /* Bind tc to vpe */
  294. write_tc_c0_tcbind(vpe);
  295. /* In general, all TCs should have the same cpu_data indications */
  296. memcpy(&cpu_data[cpu], &cpu_data[0], sizeof(struct cpuinfo_mips));
  297. /* For 34Kf, start with TC/CPU 0 as sole owner of single FPU context */
  298. if (cpu_data[0].cputype == CPU_34K ||
  299. cpu_data[0].cputype == CPU_1004K)
  300. cpu_data[cpu].options &= ~MIPS_CPU_FPU;
  301. cpu_data[cpu].vpe_id = vpe;
  302. cpu_data[cpu].tc_id = tc;
  303. /* Multi-core SMTC hasn't been tested, but be prepared */
  304. cpu_data[cpu].core = (read_vpe_c0_ebase() >> 1) & 0xff;
  305. }
  306. /*
  307. * Tweak to get Count registes in as close a sync as possible.
  308. * Value seems good for 34K-class cores.
  309. */
  310. #define CP0_SKEW 8
  311. void smtc_prepare_cpus(int cpus)
  312. {
  313. int i, vpe, tc, ntc, nvpe, tcpervpe[NR_CPUS], slop, cpu;
  314. unsigned long flags;
  315. unsigned long val;
  316. int nipi;
  317. struct smtc_ipi *pipi;
  318. /* disable interrupts so we can disable MT */
  319. local_irq_save(flags);
  320. /* disable MT so we can configure */
  321. dvpe();
  322. dmt();
  323. spin_lock_init(&freeIPIq.lock);
  324. /*
  325. * We probably don't have as many VPEs as we do SMP "CPUs",
  326. * but it's possible - and in any case we'll never use more!
  327. */
  328. for (i=0; i<NR_CPUS; i++) {
  329. IPIQ[i].head = IPIQ[i].tail = NULL;
  330. spin_lock_init(&IPIQ[i].lock);
  331. IPIQ[i].depth = 0;
  332. IPIQ[i].resched_flag = 0; /* No reschedules queued initially */
  333. }
  334. /* cpu_data index starts at zero */
  335. cpu = 0;
  336. cpu_data[cpu].vpe_id = 0;
  337. cpu_data[cpu].tc_id = 0;
  338. cpu_data[cpu].core = (read_c0_ebase() >> 1) & 0xff;
  339. cpu++;
  340. /* Report on boot-time options */
  341. mips_mt_set_cpuoptions();
  342. if (vpelimit > 0)
  343. printk("Limit of %d VPEs set\n", vpelimit);
  344. if (tclimit > 0)
  345. printk("Limit of %d TCs set\n", tclimit);
  346. if (nostlb) {
  347. printk("Shared TLB Use Inhibited - UNSAFE for Multi-VPE Operation\n");
  348. }
  349. if (asidmask)
  350. printk("ASID mask value override to 0x%x\n", asidmask);
  351. /* Temporary */
  352. #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
  353. if (hang_trig)
  354. printk("Logic Analyser Trigger on suspected TC hang\n");
  355. #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
  356. /* Put MVPE's into 'configuration state' */
  357. write_c0_mvpcontrol( read_c0_mvpcontrol() | MVPCONTROL_VPC );
  358. val = read_c0_mvpconf0();
  359. nvpe = ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1;
  360. if (vpelimit > 0 && nvpe > vpelimit)
  361. nvpe = vpelimit;
  362. ntc = ((val & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
  363. if (ntc > NR_CPUS)
  364. ntc = NR_CPUS;
  365. if (tclimit > 0 && ntc > tclimit)
  366. ntc = tclimit;
  367. slop = ntc % nvpe;
  368. for (i = 0; i < nvpe; i++) {
  369. tcpervpe[i] = ntc / nvpe;
  370. if (slop) {
  371. if((slop - i) > 0) tcpervpe[i]++;
  372. }
  373. }
  374. /* Handle command line override for VPE0 */
  375. if (vpe0limit > ntc) vpe0limit = ntc;
  376. if (vpe0limit > 0) {
  377. int slopslop;
  378. if (vpe0limit < tcpervpe[0]) {
  379. /* Reducing TC count - distribute to others */
  380. slop = tcpervpe[0] - vpe0limit;
  381. slopslop = slop % (nvpe - 1);
  382. tcpervpe[0] = vpe0limit;
  383. for (i = 1; i < nvpe; i++) {
  384. tcpervpe[i] += slop / (nvpe - 1);
  385. if(slopslop && ((slopslop - (i - 1) > 0)))
  386. tcpervpe[i]++;
  387. }
  388. } else if (vpe0limit > tcpervpe[0]) {
  389. /* Increasing TC count - steal from others */
  390. slop = vpe0limit - tcpervpe[0];
  391. slopslop = slop % (nvpe - 1);
  392. tcpervpe[0] = vpe0limit;
  393. for (i = 1; i < nvpe; i++) {
  394. tcpervpe[i] -= slop / (nvpe - 1);
  395. if(slopslop && ((slopslop - (i - 1) > 0)))
  396. tcpervpe[i]--;
  397. }
  398. }
  399. }
  400. /* Set up shared TLB */
  401. smtc_configure_tlb();
  402. for (tc = 0, vpe = 0 ; (vpe < nvpe) && (tc < ntc) ; vpe++) {
  403. if (tcpervpe[vpe] == 0)
  404. continue;
  405. if (vpe != 0)
  406. printk(", ");
  407. printk("VPE %d: TC", vpe);
  408. for (i = 0; i < tcpervpe[vpe]; i++) {
  409. /*
  410. * TC 0 is bound to VPE 0 at reset,
  411. * and is presumably executing this
  412. * code. Leave it alone!
  413. */
  414. if (tc != 0) {
  415. smtc_tc_setup(vpe, tc, cpu);
  416. cpu++;
  417. }
  418. printk(" %d", tc);
  419. tc++;
  420. }
  421. if (vpe != 0) {
  422. /*
  423. * Allow this VPE to control others.
  424. */
  425. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() |
  426. VPECONF0_MVP);
  427. /*
  428. * Clear any stale software interrupts from VPE's Cause
  429. */
  430. write_vpe_c0_cause(0);
  431. /*
  432. * Clear ERL/EXL of VPEs other than 0
  433. * and set restricted interrupt enable/mask.
  434. */
  435. write_vpe_c0_status((read_vpe_c0_status()
  436. & ~(ST0_BEV | ST0_ERL | ST0_EXL | ST0_IM))
  437. | (STATUSF_IP0 | STATUSF_IP1 | STATUSF_IP7
  438. | ST0_IE));
  439. /*
  440. * set config to be the same as vpe0,
  441. * particularly kseg0 coherency alg
  442. */
  443. write_vpe_c0_config(read_c0_config());
  444. /* Clear any pending timer interrupt */
  445. write_vpe_c0_compare(0);
  446. /* Propagate Config7 */
  447. write_vpe_c0_config7(read_c0_config7());
  448. write_vpe_c0_count(read_c0_count() + CP0_SKEW);
  449. ehb();
  450. }
  451. /* enable multi-threading within VPE */
  452. write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() | VPECONTROL_TE);
  453. /* enable the VPE */
  454. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA);
  455. }
  456. /*
  457. * Pull any physically present but unused TCs out of circulation.
  458. */
  459. while (tc < (((val & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1)) {
  460. set_cpu_possible(tc, false);
  461. set_cpu_present(tc, false);
  462. tc++;
  463. }
  464. /* release config state */
  465. write_c0_mvpcontrol( read_c0_mvpcontrol() & ~ MVPCONTROL_VPC );
  466. printk("\n");
  467. /* Set up coprocessor affinity CPU mask(s) */
  468. #ifdef CONFIG_MIPS_MT_FPAFF
  469. for (tc = 0; tc < ntc; tc++) {
  470. if (cpu_data[tc].options & MIPS_CPU_FPU)
  471. cpu_set(tc, mt_fpu_cpumask);
  472. }
  473. #endif
  474. /* set up ipi interrupts... */
  475. /* If we have multiple VPEs running, set up the cross-VPE interrupt */
  476. setup_cross_vpe_interrupts(nvpe);
  477. /* Set up queue of free IPI "messages". */
  478. nipi = NR_CPUS * IPIBUF_PER_CPU;
  479. if (ipibuffers > 0)
  480. nipi = ipibuffers;
  481. pipi = kmalloc(nipi *sizeof(struct smtc_ipi), GFP_KERNEL);
  482. if (pipi == NULL)
  483. panic("kmalloc of IPI message buffers failed\n");
  484. else
  485. printk("IPI buffer pool of %d buffers\n", nipi);
  486. for (i = 0; i < nipi; i++) {
  487. smtc_ipi_nq(&freeIPIq, pipi);
  488. pipi++;
  489. }
  490. /* Arm multithreading and enable other VPEs - but all TCs are Halted */
  491. emt(EMT_ENABLE);
  492. evpe(EVPE_ENABLE);
  493. local_irq_restore(flags);
  494. /* Initialize SMTC /proc statistics/diagnostics */
  495. init_smtc_stats();
  496. }
  497. /*
  498. * Setup the PC, SP, and GP of a secondary processor and start it
  499. * running!
  500. * smp_bootstrap is the place to resume from
  501. * __KSTK_TOS(idle) is apparently the stack pointer
  502. * (unsigned long)idle->thread_info the gp
  503. *
  504. */
  505. void __cpuinit smtc_boot_secondary(int cpu, struct task_struct *idle)
  506. {
  507. extern u32 kernelsp[NR_CPUS];
  508. unsigned long flags;
  509. int mtflags;
  510. LOCK_MT_PRA();
  511. if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
  512. dvpe();
  513. }
  514. settc(cpu_data[cpu].tc_id);
  515. /* pc */
  516. write_tc_c0_tcrestart((unsigned long)&smp_bootstrap);
  517. /* stack pointer */
  518. kernelsp[cpu] = __KSTK_TOS(idle);
  519. write_tc_gpr_sp(__KSTK_TOS(idle));
  520. /* global pointer */
  521. write_tc_gpr_gp((unsigned long)task_thread_info(idle));
  522. smtc_status |= SMTC_MTC_ACTIVE;
  523. write_tc_c0_tchalt(0);
  524. if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
  525. evpe(EVPE_ENABLE);
  526. }
  527. UNLOCK_MT_PRA();
  528. }
  529. void smtc_init_secondary(void)
  530. {
  531. local_irq_enable();
  532. }
  533. void smtc_smp_finish(void)
  534. {
  535. int cpu = smp_processor_id();
  536. /*
  537. * Lowest-numbered CPU per VPE starts a clock tick.
  538. * Like per_cpu_trap_init() hack, this assumes that
  539. * SMTC init code assigns TCs consdecutively and
  540. * in ascending order across available VPEs.
  541. */
  542. if (cpu > 0 && (cpu_data[cpu].vpe_id != cpu_data[cpu - 1].vpe_id))
  543. write_c0_compare(read_c0_count() + mips_hpt_frequency/HZ);
  544. printk("TC %d going on-line as CPU %d\n",
  545. cpu_data[smp_processor_id()].tc_id, smp_processor_id());
  546. }
  547. void smtc_cpus_done(void)
  548. {
  549. }
  550. /*
  551. * Support for SMTC-optimized driver IRQ registration
  552. */
  553. /*
  554. * SMTC Kernel needs to manipulate low-level CPU interrupt mask
  555. * in do_IRQ. These are passed in setup_irq_smtc() and stored
  556. * in this table.
  557. */
  558. int setup_irq_smtc(unsigned int irq, struct irqaction * new,
  559. unsigned long hwmask)
  560. {
  561. #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
  562. unsigned int vpe = current_cpu_data.vpe_id;
  563. vpemask[vpe][irq - MIPS_CPU_IRQ_BASE] = 1;
  564. #endif
  565. irq_hwmask[irq] = hwmask;
  566. return setup_irq(irq, new);
  567. }
  568. #ifdef CONFIG_MIPS_MT_SMTC_IRQAFF
  569. /*
  570. * Support for IRQ affinity to TCs
  571. */
  572. void smtc_set_irq_affinity(unsigned int irq, cpumask_t affinity)
  573. {
  574. /*
  575. * If a "fast path" cache of quickly decodable affinity state
  576. * is maintained, this is where it gets done, on a call up
  577. * from the platform affinity code.
  578. */
  579. }
  580. void smtc_forward_irq(struct irq_data *d)
  581. {
  582. unsigned int irq = d->irq;
  583. int target;
  584. /*
  585. * OK wise guy, now figure out how to get the IRQ
  586. * to be serviced on an authorized "CPU".
  587. *
  588. * Ideally, to handle the situation where an IRQ has multiple
  589. * eligible CPUS, we would maintain state per IRQ that would
  590. * allow a fair distribution of service requests. Since the
  591. * expected use model is any-or-only-one, for simplicity
  592. * and efficiency, we just pick the easiest one to find.
  593. */
  594. target = cpumask_first(d->affinity);
  595. /*
  596. * We depend on the platform code to have correctly processed
  597. * IRQ affinity change requests to ensure that the IRQ affinity
  598. * mask has been purged of bits corresponding to nonexistent and
  599. * offline "CPUs", and to TCs bound to VPEs other than the VPE
  600. * connected to the physical interrupt input for the interrupt
  601. * in question. Otherwise we have a nasty problem with interrupt
  602. * mask management. This is best handled in non-performance-critical
  603. * platform IRQ affinity setting code, to minimize interrupt-time
  604. * checks.
  605. */
  606. /* If no one is eligible, service locally */
  607. if (target >= NR_CPUS)
  608. do_IRQ_no_affinity(irq);
  609. else
  610. smtc_send_ipi(target, IRQ_AFFINITY_IPI, irq);
  611. }
  612. #endif /* CONFIG_MIPS_MT_SMTC_IRQAFF */
  613. /*
  614. * IPI model for SMTC is tricky, because interrupts aren't TC-specific.
  615. * Within a VPE one TC can interrupt another by different approaches.
  616. * The easiest to get right would probably be to make all TCs except
  617. * the target IXMT and set a software interrupt, but an IXMT-based
  618. * scheme requires that a handler must run before a new IPI could
  619. * be sent, which would break the "broadcast" loops in MIPS MT.
  620. * A more gonzo approach within a VPE is to halt the TC, extract
  621. * its Restart, Status, and a couple of GPRs, and program the Restart
  622. * address to emulate an interrupt.
  623. *
  624. * Within a VPE, one can be confident that the target TC isn't in
  625. * a critical EXL state when halted, since the write to the Halt
  626. * register could not have issued on the writing thread if the
  627. * halting thread had EXL set. So k0 and k1 of the target TC
  628. * can be used by the injection code. Across VPEs, one can't
  629. * be certain that the target TC isn't in a critical exception
  630. * state. So we try a two-step process of sending a software
  631. * interrupt to the target VPE, which either handles the event
  632. * itself (if it was the target) or injects the event within
  633. * the VPE.
  634. */
  635. static void smtc_ipi_qdump(void)
  636. {
  637. int i;
  638. struct smtc_ipi *temp;
  639. for (i = 0; i < NR_CPUS ;i++) {
  640. pr_info("IPIQ[%d]: head = 0x%x, tail = 0x%x, depth = %d\n",
  641. i, (unsigned)IPIQ[i].head, (unsigned)IPIQ[i].tail,
  642. IPIQ[i].depth);
  643. temp = IPIQ[i].head;
  644. while (temp != IPIQ[i].tail) {
  645. pr_debug("%d %d %d: ", temp->type, temp->dest,
  646. (int)temp->arg);
  647. #ifdef SMTC_IPI_DEBUG
  648. pr_debug("%u %lu\n", temp->sender, temp->stamp);
  649. #else
  650. pr_debug("\n");
  651. #endif
  652. temp = temp->flink;
  653. }
  654. }
  655. }
  656. /*
  657. * The standard atomic.h primitives don't quite do what we want
  658. * here: We need an atomic add-and-return-previous-value (which
  659. * could be done with atomic_add_return and a decrement) and an
  660. * atomic set/zero-and-return-previous-value (which can't really
  661. * be done with the atomic.h primitives). And since this is
  662. * MIPS MT, we can assume that we have LL/SC.
  663. */
  664. static inline int atomic_postincrement(atomic_t *v)
  665. {
  666. unsigned long result;
  667. unsigned long temp;
  668. __asm__ __volatile__(
  669. "1: ll %0, %2 \n"
  670. " addu %1, %0, 1 \n"
  671. " sc %1, %2 \n"
  672. " beqz %1, 1b \n"
  673. __WEAK_LLSC_MB
  674. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  675. : "m" (v->counter)
  676. : "memory");
  677. return result;
  678. }
  679. void smtc_send_ipi(int cpu, int type, unsigned int action)
  680. {
  681. int tcstatus;
  682. struct smtc_ipi *pipi;
  683. unsigned long flags;
  684. int mtflags;
  685. unsigned long tcrestart;
  686. extern void r4k_wait_irqoff(void), __pastwait(void);
  687. int set_resched_flag = (type == LINUX_SMP_IPI &&
  688. action == SMP_RESCHEDULE_YOURSELF);
  689. if (cpu == smp_processor_id()) {
  690. printk("Cannot Send IPI to self!\n");
  691. return;
  692. }
  693. if (set_resched_flag && IPIQ[cpu].resched_flag != 0)
  694. return; /* There is a reschedule queued already */
  695. /* Set up a descriptor, to be delivered either promptly or queued */
  696. pipi = smtc_ipi_dq(&freeIPIq);
  697. if (pipi == NULL) {
  698. bust_spinlocks(1);
  699. mips_mt_regdump(dvpe());
  700. panic("IPI Msg. Buffers Depleted\n");
  701. }
  702. pipi->type = type;
  703. pipi->arg = (void *)action;
  704. pipi->dest = cpu;
  705. if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
  706. /* If not on same VPE, enqueue and send cross-VPE interrupt */
  707. IPIQ[cpu].resched_flag |= set_resched_flag;
  708. smtc_ipi_nq(&IPIQ[cpu], pipi);
  709. LOCK_CORE_PRA();
  710. settc(cpu_data[cpu].tc_id);
  711. write_vpe_c0_cause(read_vpe_c0_cause() | C_SW1);
  712. UNLOCK_CORE_PRA();
  713. } else {
  714. /*
  715. * Not sufficient to do a LOCK_MT_PRA (dmt) here,
  716. * since ASID shootdown on the other VPE may
  717. * collide with this operation.
  718. */
  719. LOCK_CORE_PRA();
  720. settc(cpu_data[cpu].tc_id);
  721. /* Halt the targeted TC */
  722. write_tc_c0_tchalt(TCHALT_H);
  723. mips_ihb();
  724. /*
  725. * Inspect TCStatus - if IXMT is set, we have to queue
  726. * a message. Otherwise, we set up the "interrupt"
  727. * of the other TC
  728. */
  729. tcstatus = read_tc_c0_tcstatus();
  730. if ((tcstatus & TCSTATUS_IXMT) != 0) {
  731. /*
  732. * If we're in the the irq-off version of the wait
  733. * loop, we need to force exit from the wait and
  734. * do a direct post of the IPI.
  735. */
  736. if (cpu_wait == r4k_wait_irqoff) {
  737. tcrestart = read_tc_c0_tcrestart();
  738. if (tcrestart >= (unsigned long)r4k_wait_irqoff
  739. && tcrestart < (unsigned long)__pastwait) {
  740. write_tc_c0_tcrestart(__pastwait);
  741. tcstatus &= ~TCSTATUS_IXMT;
  742. write_tc_c0_tcstatus(tcstatus);
  743. goto postdirect;
  744. }
  745. }
  746. /*
  747. * Otherwise we queue the message for the target TC
  748. * to pick up when he does a local_irq_restore()
  749. */
  750. write_tc_c0_tchalt(0);
  751. UNLOCK_CORE_PRA();
  752. IPIQ[cpu].resched_flag |= set_resched_flag;
  753. smtc_ipi_nq(&IPIQ[cpu], pipi);
  754. } else {
  755. postdirect:
  756. post_direct_ipi(cpu, pipi);
  757. write_tc_c0_tchalt(0);
  758. UNLOCK_CORE_PRA();
  759. }
  760. }
  761. }
  762. /*
  763. * Send IPI message to Halted TC, TargTC/TargVPE already having been set
  764. */
  765. static void post_direct_ipi(int cpu, struct smtc_ipi *pipi)
  766. {
  767. struct pt_regs *kstack;
  768. unsigned long tcstatus;
  769. unsigned long tcrestart;
  770. extern u32 kernelsp[NR_CPUS];
  771. extern void __smtc_ipi_vector(void);
  772. //printk("%s: on %d for %d\n", __func__, smp_processor_id(), cpu);
  773. /* Extract Status, EPC from halted TC */
  774. tcstatus = read_tc_c0_tcstatus();
  775. tcrestart = read_tc_c0_tcrestart();
  776. /* If TCRestart indicates a WAIT instruction, advance the PC */
  777. if ((tcrestart & 0x80000000)
  778. && ((*(unsigned int *)tcrestart & 0xfe00003f) == 0x42000020)) {
  779. tcrestart += 4;
  780. }
  781. /*
  782. * Save on TC's future kernel stack
  783. *
  784. * CU bit of Status is indicator that TC was
  785. * already running on a kernel stack...
  786. */
  787. if (tcstatus & ST0_CU0) {
  788. /* Note that this "- 1" is pointer arithmetic */
  789. kstack = ((struct pt_regs *)read_tc_gpr_sp()) - 1;
  790. } else {
  791. kstack = ((struct pt_regs *)kernelsp[cpu]) - 1;
  792. }
  793. kstack->cp0_epc = (long)tcrestart;
  794. /* Save TCStatus */
  795. kstack->cp0_tcstatus = tcstatus;
  796. /* Pass token of operation to be performed kernel stack pad area */
  797. kstack->pad0[4] = (unsigned long)pipi;
  798. /* Pass address of function to be called likewise */
  799. kstack->pad0[5] = (unsigned long)&ipi_decode;
  800. /* Set interrupt exempt and kernel mode */
  801. tcstatus |= TCSTATUS_IXMT;
  802. tcstatus &= ~TCSTATUS_TKSU;
  803. write_tc_c0_tcstatus(tcstatus);
  804. ehb();
  805. /* Set TC Restart address to be SMTC IPI vector */
  806. write_tc_c0_tcrestart(__smtc_ipi_vector);
  807. }
  808. static void ipi_resched_interrupt(void)
  809. {
  810. scheduler_ipi();
  811. }
  812. static void ipi_call_interrupt(void)
  813. {
  814. /* Invoke generic function invocation code in smp.c */
  815. smp_call_function_interrupt();
  816. }
  817. DECLARE_PER_CPU(struct clock_event_device, mips_clockevent_device);
  818. static void __irq_entry smtc_clock_tick_interrupt(void)
  819. {
  820. unsigned int cpu = smp_processor_id();
  821. struct clock_event_device *cd;
  822. int irq = MIPS_CPU_IRQ_BASE + 1;
  823. irq_enter();
  824. kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq));
  825. cd = &per_cpu(mips_clockevent_device, cpu);
  826. cd->event_handler(cd);
  827. irq_exit();
  828. }
  829. void ipi_decode(struct smtc_ipi *pipi)
  830. {
  831. void *arg_copy = pipi->arg;
  832. int type_copy = pipi->type;
  833. smtc_ipi_nq(&freeIPIq, pipi);
  834. switch (type_copy) {
  835. case SMTC_CLOCK_TICK:
  836. smtc_clock_tick_interrupt();
  837. break;
  838. case LINUX_SMP_IPI:
  839. switch ((int)arg_copy) {
  840. case SMP_RESCHEDULE_YOURSELF:
  841. ipi_resched_interrupt();
  842. break;
  843. case SMP_CALL_FUNCTION:
  844. ipi_call_interrupt();
  845. break;
  846. default:
  847. printk("Impossible SMTC IPI Argument %p\n", 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. __arch_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. irq_set_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. __arch_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. }