smtc.c 38 KB

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