smp.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. /* smp.c: Sparc64 SMP support.
  2. *
  3. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/threads.h>
  11. #include <linux/smp.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/delay.h>
  15. #include <linux/init.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/fs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/cache.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/profile.h>
  22. #include <linux/bootmem.h>
  23. #include <asm/head.h>
  24. #include <asm/ptrace.h>
  25. #include <asm/atomic.h>
  26. #include <asm/tlbflush.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/cpudata.h>
  29. #include <asm/irq.h>
  30. #include <asm/irq_regs.h>
  31. #include <asm/page.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/oplib.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/timer.h>
  36. #include <asm/starfire.h>
  37. #include <asm/tlb.h>
  38. #include <asm/sections.h>
  39. #include <asm/prom.h>
  40. #include <asm/mdesc.h>
  41. extern void calibrate_delay(void);
  42. int sparc64_multi_core __read_mostly;
  43. /* Please don't make this stuff initdata!!! --DaveM */
  44. unsigned char boot_cpu_id;
  45. cpumask_t cpu_online_map __read_mostly = CPU_MASK_NONE;
  46. cpumask_t phys_cpu_present_map __read_mostly = CPU_MASK_NONE;
  47. cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly =
  48. { [0 ... NR_CPUS-1] = CPU_MASK_NONE };
  49. cpumask_t cpu_core_map[NR_CPUS] __read_mostly =
  50. { [0 ... NR_CPUS-1] = CPU_MASK_NONE };
  51. static cpumask_t smp_commenced_mask;
  52. static cpumask_t cpu_callout_map;
  53. void smp_info(struct seq_file *m)
  54. {
  55. int i;
  56. seq_printf(m, "State:\n");
  57. for_each_online_cpu(i)
  58. seq_printf(m, "CPU%d:\t\tonline\n", i);
  59. }
  60. void smp_bogo(struct seq_file *m)
  61. {
  62. int i;
  63. for_each_online_cpu(i)
  64. seq_printf(m,
  65. "Cpu%dBogo\t: %lu.%02lu\n"
  66. "Cpu%dClkTck\t: %016lx\n",
  67. i, cpu_data(i).udelay_val / (500000/HZ),
  68. (cpu_data(i).udelay_val / (5000/HZ)) % 100,
  69. i, cpu_data(i).clock_tick);
  70. }
  71. extern void setup_sparc64_timer(void);
  72. static volatile unsigned long callin_flag = 0;
  73. void __init smp_callin(void)
  74. {
  75. int cpuid = hard_smp_processor_id();
  76. __local_per_cpu_offset = __per_cpu_offset(cpuid);
  77. if (tlb_type == hypervisor)
  78. sun4v_ktsb_register();
  79. __flush_tlb_all();
  80. setup_sparc64_timer();
  81. if (cheetah_pcache_forced_on)
  82. cheetah_enable_pcache();
  83. local_irq_enable();
  84. calibrate_delay();
  85. cpu_data(cpuid).udelay_val = loops_per_jiffy;
  86. callin_flag = 1;
  87. __asm__ __volatile__("membar #Sync\n\t"
  88. "flush %%g6" : : : "memory");
  89. /* Clear this or we will die instantly when we
  90. * schedule back to this idler...
  91. */
  92. current_thread_info()->new_child = 0;
  93. /* Attach to the address space of init_task. */
  94. atomic_inc(&init_mm.mm_count);
  95. current->active_mm = &init_mm;
  96. while (!cpu_isset(cpuid, smp_commenced_mask))
  97. rmb();
  98. cpu_set(cpuid, cpu_online_map);
  99. /* idle thread is expected to have preempt disabled */
  100. preempt_disable();
  101. }
  102. void cpu_panic(void)
  103. {
  104. printk("CPU[%d]: Returns from cpu_idle!\n", smp_processor_id());
  105. panic("SMP bolixed\n");
  106. }
  107. /* This tick register synchronization scheme is taken entirely from
  108. * the ia64 port, see arch/ia64/kernel/smpboot.c for details and credit.
  109. *
  110. * The only change I've made is to rework it so that the master
  111. * initiates the synchonization instead of the slave. -DaveM
  112. */
  113. #define MASTER 0
  114. #define SLAVE (SMP_CACHE_BYTES/sizeof(unsigned long))
  115. #define NUM_ROUNDS 64 /* magic value */
  116. #define NUM_ITERS 5 /* likewise */
  117. static DEFINE_SPINLOCK(itc_sync_lock);
  118. static unsigned long go[SLAVE + 1];
  119. #define DEBUG_TICK_SYNC 0
  120. static inline long get_delta (long *rt, long *master)
  121. {
  122. unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;
  123. unsigned long tcenter, t0, t1, tm;
  124. unsigned long i;
  125. for (i = 0; i < NUM_ITERS; i++) {
  126. t0 = tick_ops->get_tick();
  127. go[MASTER] = 1;
  128. membar_storeload();
  129. while (!(tm = go[SLAVE]))
  130. rmb();
  131. go[SLAVE] = 0;
  132. wmb();
  133. t1 = tick_ops->get_tick();
  134. if (t1 - t0 < best_t1 - best_t0)
  135. best_t0 = t0, best_t1 = t1, best_tm = tm;
  136. }
  137. *rt = best_t1 - best_t0;
  138. *master = best_tm - best_t0;
  139. /* average best_t0 and best_t1 without overflow: */
  140. tcenter = (best_t0/2 + best_t1/2);
  141. if (best_t0 % 2 + best_t1 % 2 == 2)
  142. tcenter++;
  143. return tcenter - best_tm;
  144. }
  145. void smp_synchronize_tick_client(void)
  146. {
  147. long i, delta, adj, adjust_latency = 0, done = 0;
  148. unsigned long flags, rt, master_time_stamp, bound;
  149. #if DEBUG_TICK_SYNC
  150. struct {
  151. long rt; /* roundtrip time */
  152. long master; /* master's timestamp */
  153. long diff; /* difference between midpoint and master's timestamp */
  154. long lat; /* estimate of itc adjustment latency */
  155. } t[NUM_ROUNDS];
  156. #endif
  157. go[MASTER] = 1;
  158. while (go[MASTER])
  159. rmb();
  160. local_irq_save(flags);
  161. {
  162. for (i = 0; i < NUM_ROUNDS; i++) {
  163. delta = get_delta(&rt, &master_time_stamp);
  164. if (delta == 0) {
  165. done = 1; /* let's lock on to this... */
  166. bound = rt;
  167. }
  168. if (!done) {
  169. if (i > 0) {
  170. adjust_latency += -delta;
  171. adj = -delta + adjust_latency/4;
  172. } else
  173. adj = -delta;
  174. tick_ops->add_tick(adj);
  175. }
  176. #if DEBUG_TICK_SYNC
  177. t[i].rt = rt;
  178. t[i].master = master_time_stamp;
  179. t[i].diff = delta;
  180. t[i].lat = adjust_latency/4;
  181. #endif
  182. }
  183. }
  184. local_irq_restore(flags);
  185. #if DEBUG_TICK_SYNC
  186. for (i = 0; i < NUM_ROUNDS; i++)
  187. printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n",
  188. t[i].rt, t[i].master, t[i].diff, t[i].lat);
  189. #endif
  190. printk(KERN_INFO "CPU %d: synchronized TICK with master CPU (last diff %ld cycles,"
  191. "maxerr %lu cycles)\n", smp_processor_id(), delta, rt);
  192. }
  193. static void smp_start_sync_tick_client(int cpu);
  194. static void smp_synchronize_one_tick(int cpu)
  195. {
  196. unsigned long flags, i;
  197. go[MASTER] = 0;
  198. smp_start_sync_tick_client(cpu);
  199. /* wait for client to be ready */
  200. while (!go[MASTER])
  201. rmb();
  202. /* now let the client proceed into his loop */
  203. go[MASTER] = 0;
  204. membar_storeload();
  205. spin_lock_irqsave(&itc_sync_lock, flags);
  206. {
  207. for (i = 0; i < NUM_ROUNDS*NUM_ITERS; i++) {
  208. while (!go[MASTER])
  209. rmb();
  210. go[MASTER] = 0;
  211. wmb();
  212. go[SLAVE] = tick_ops->get_tick();
  213. membar_storeload();
  214. }
  215. }
  216. spin_unlock_irqrestore(&itc_sync_lock, flags);
  217. }
  218. extern void sun4v_init_mondo_queues(int use_bootmem, int cpu, int alloc, int load);
  219. extern unsigned long sparc64_cpu_startup;
  220. /* The OBP cpu startup callback truncates the 3rd arg cookie to
  221. * 32-bits (I think) so to be safe we have it read the pointer
  222. * contained here so we work on >4GB machines. -DaveM
  223. */
  224. static struct thread_info *cpu_new_thread = NULL;
  225. static int __devinit smp_boot_one_cpu(unsigned int cpu)
  226. {
  227. unsigned long entry =
  228. (unsigned long)(&sparc64_cpu_startup);
  229. unsigned long cookie =
  230. (unsigned long)(&cpu_new_thread);
  231. struct task_struct *p;
  232. int timeout, ret;
  233. p = fork_idle(cpu);
  234. callin_flag = 0;
  235. cpu_new_thread = task_thread_info(p);
  236. cpu_set(cpu, cpu_callout_map);
  237. if (tlb_type == hypervisor) {
  238. /* Alloc the mondo queues, cpu will load them. */
  239. sun4v_init_mondo_queues(0, cpu, 1, 0);
  240. prom_startcpu_cpuid(cpu, entry, cookie);
  241. } else {
  242. struct device_node *dp = of_find_node_by_cpuid(cpu);
  243. prom_startcpu(dp->node, entry, cookie);
  244. }
  245. for (timeout = 0; timeout < 5000000; timeout++) {
  246. if (callin_flag)
  247. break;
  248. udelay(100);
  249. }
  250. if (callin_flag) {
  251. ret = 0;
  252. } else {
  253. printk("Processor %d is stuck.\n", cpu);
  254. cpu_clear(cpu, cpu_callout_map);
  255. ret = -ENODEV;
  256. }
  257. cpu_new_thread = NULL;
  258. return ret;
  259. }
  260. static void spitfire_xcall_helper(u64 data0, u64 data1, u64 data2, u64 pstate, unsigned long cpu)
  261. {
  262. u64 result, target;
  263. int stuck, tmp;
  264. if (this_is_starfire) {
  265. /* map to real upaid */
  266. cpu = (((cpu & 0x3c) << 1) |
  267. ((cpu & 0x40) >> 4) |
  268. (cpu & 0x3));
  269. }
  270. target = (cpu << 14) | 0x70;
  271. again:
  272. /* Ok, this is the real Spitfire Errata #54.
  273. * One must read back from a UDB internal register
  274. * after writes to the UDB interrupt dispatch, but
  275. * before the membar Sync for that write.
  276. * So we use the high UDB control register (ASI 0x7f,
  277. * ADDR 0x20) for the dummy read. -DaveM
  278. */
  279. tmp = 0x40;
  280. __asm__ __volatile__(
  281. "wrpr %1, %2, %%pstate\n\t"
  282. "stxa %4, [%0] %3\n\t"
  283. "stxa %5, [%0+%8] %3\n\t"
  284. "add %0, %8, %0\n\t"
  285. "stxa %6, [%0+%8] %3\n\t"
  286. "membar #Sync\n\t"
  287. "stxa %%g0, [%7] %3\n\t"
  288. "membar #Sync\n\t"
  289. "mov 0x20, %%g1\n\t"
  290. "ldxa [%%g1] 0x7f, %%g0\n\t"
  291. "membar #Sync"
  292. : "=r" (tmp)
  293. : "r" (pstate), "i" (PSTATE_IE), "i" (ASI_INTR_W),
  294. "r" (data0), "r" (data1), "r" (data2), "r" (target),
  295. "r" (0x10), "0" (tmp)
  296. : "g1");
  297. /* NOTE: PSTATE_IE is still clear. */
  298. stuck = 100000;
  299. do {
  300. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  301. : "=r" (result)
  302. : "i" (ASI_INTR_DISPATCH_STAT));
  303. if (result == 0) {
  304. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  305. : : "r" (pstate));
  306. return;
  307. }
  308. stuck -= 1;
  309. if (stuck == 0)
  310. break;
  311. } while (result & 0x1);
  312. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  313. : : "r" (pstate));
  314. if (stuck == 0) {
  315. printk("CPU[%d]: mondo stuckage result[%016lx]\n",
  316. smp_processor_id(), result);
  317. } else {
  318. udelay(2);
  319. goto again;
  320. }
  321. }
  322. static __inline__ void spitfire_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask)
  323. {
  324. u64 pstate;
  325. int i;
  326. __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
  327. for_each_cpu_mask(i, mask)
  328. spitfire_xcall_helper(data0, data1, data2, pstate, i);
  329. }
  330. /* Cheetah now allows to send the whole 64-bytes of data in the interrupt
  331. * packet, but we have no use for that. However we do take advantage of
  332. * the new pipelining feature (ie. dispatch to multiple cpus simultaneously).
  333. */
  334. static void cheetah_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask)
  335. {
  336. u64 pstate, ver;
  337. int nack_busy_id, is_jbus, need_more;
  338. if (cpus_empty(mask))
  339. return;
  340. /* Unfortunately, someone at Sun had the brilliant idea to make the
  341. * busy/nack fields hard-coded by ITID number for this Ultra-III
  342. * derivative processor.
  343. */
  344. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  345. is_jbus = ((ver >> 32) == __JALAPENO_ID ||
  346. (ver >> 32) == __SERRANO_ID);
  347. __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
  348. retry:
  349. need_more = 0;
  350. __asm__ __volatile__("wrpr %0, %1, %%pstate\n\t"
  351. : : "r" (pstate), "i" (PSTATE_IE));
  352. /* Setup the dispatch data registers. */
  353. __asm__ __volatile__("stxa %0, [%3] %6\n\t"
  354. "stxa %1, [%4] %6\n\t"
  355. "stxa %2, [%5] %6\n\t"
  356. "membar #Sync\n\t"
  357. : /* no outputs */
  358. : "r" (data0), "r" (data1), "r" (data2),
  359. "r" (0x40), "r" (0x50), "r" (0x60),
  360. "i" (ASI_INTR_W));
  361. nack_busy_id = 0;
  362. {
  363. int i;
  364. for_each_cpu_mask(i, mask) {
  365. u64 target = (i << 14) | 0x70;
  366. if (!is_jbus)
  367. target |= (nack_busy_id << 24);
  368. __asm__ __volatile__(
  369. "stxa %%g0, [%0] %1\n\t"
  370. "membar #Sync\n\t"
  371. : /* no outputs */
  372. : "r" (target), "i" (ASI_INTR_W));
  373. nack_busy_id++;
  374. if (nack_busy_id == 32) {
  375. need_more = 1;
  376. break;
  377. }
  378. }
  379. }
  380. /* Now, poll for completion. */
  381. {
  382. u64 dispatch_stat;
  383. long stuck;
  384. stuck = 100000 * nack_busy_id;
  385. do {
  386. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  387. : "=r" (dispatch_stat)
  388. : "i" (ASI_INTR_DISPATCH_STAT));
  389. if (dispatch_stat == 0UL) {
  390. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  391. : : "r" (pstate));
  392. if (unlikely(need_more)) {
  393. int i, cnt = 0;
  394. for_each_cpu_mask(i, mask) {
  395. cpu_clear(i, mask);
  396. cnt++;
  397. if (cnt == 32)
  398. break;
  399. }
  400. goto retry;
  401. }
  402. return;
  403. }
  404. if (!--stuck)
  405. break;
  406. } while (dispatch_stat & 0x5555555555555555UL);
  407. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  408. : : "r" (pstate));
  409. if ((dispatch_stat & ~(0x5555555555555555UL)) == 0) {
  410. /* Busy bits will not clear, continue instead
  411. * of freezing up on this cpu.
  412. */
  413. printk("CPU[%d]: mondo stuckage result[%016lx]\n",
  414. smp_processor_id(), dispatch_stat);
  415. } else {
  416. int i, this_busy_nack = 0;
  417. /* Delay some random time with interrupts enabled
  418. * to prevent deadlock.
  419. */
  420. udelay(2 * nack_busy_id);
  421. /* Clear out the mask bits for cpus which did not
  422. * NACK us.
  423. */
  424. for_each_cpu_mask(i, mask) {
  425. u64 check_mask;
  426. if (is_jbus)
  427. check_mask = (0x2UL << (2*i));
  428. else
  429. check_mask = (0x2UL <<
  430. this_busy_nack);
  431. if ((dispatch_stat & check_mask) == 0)
  432. cpu_clear(i, mask);
  433. this_busy_nack += 2;
  434. if (this_busy_nack == 64)
  435. break;
  436. }
  437. goto retry;
  438. }
  439. }
  440. }
  441. /* Multi-cpu list version. */
  442. static void hypervisor_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask)
  443. {
  444. struct trap_per_cpu *tb;
  445. u16 *cpu_list;
  446. u64 *mondo;
  447. cpumask_t error_mask;
  448. unsigned long flags, status;
  449. int cnt, retries, this_cpu, prev_sent, i;
  450. if (cpus_empty(mask))
  451. return;
  452. /* We have to do this whole thing with interrupts fully disabled.
  453. * Otherwise if we send an xcall from interrupt context it will
  454. * corrupt both our mondo block and cpu list state.
  455. *
  456. * One consequence of this is that we cannot use timeout mechanisms
  457. * that depend upon interrupts being delivered locally. So, for
  458. * example, we cannot sample jiffies and expect it to advance.
  459. *
  460. * Fortunately, udelay() uses %stick/%tick so we can use that.
  461. */
  462. local_irq_save(flags);
  463. this_cpu = smp_processor_id();
  464. tb = &trap_block[this_cpu];
  465. mondo = __va(tb->cpu_mondo_block_pa);
  466. mondo[0] = data0;
  467. mondo[1] = data1;
  468. mondo[2] = data2;
  469. wmb();
  470. cpu_list = __va(tb->cpu_list_pa);
  471. /* Setup the initial cpu list. */
  472. cnt = 0;
  473. for_each_cpu_mask(i, mask)
  474. cpu_list[cnt++] = i;
  475. cpus_clear(error_mask);
  476. retries = 0;
  477. prev_sent = 0;
  478. do {
  479. int forward_progress, n_sent;
  480. status = sun4v_cpu_mondo_send(cnt,
  481. tb->cpu_list_pa,
  482. tb->cpu_mondo_block_pa);
  483. /* HV_EOK means all cpus received the xcall, we're done. */
  484. if (likely(status == HV_EOK))
  485. break;
  486. /* First, see if we made any forward progress.
  487. *
  488. * The hypervisor indicates successful sends by setting
  489. * cpu list entries to the value 0xffff.
  490. */
  491. n_sent = 0;
  492. for (i = 0; i < cnt; i++) {
  493. if (likely(cpu_list[i] == 0xffff))
  494. n_sent++;
  495. }
  496. forward_progress = 0;
  497. if (n_sent > prev_sent)
  498. forward_progress = 1;
  499. prev_sent = n_sent;
  500. /* If we get a HV_ECPUERROR, then one or more of the cpus
  501. * in the list are in error state. Use the cpu_state()
  502. * hypervisor call to find out which cpus are in error state.
  503. */
  504. if (unlikely(status == HV_ECPUERROR)) {
  505. for (i = 0; i < cnt; i++) {
  506. long err;
  507. u16 cpu;
  508. cpu = cpu_list[i];
  509. if (cpu == 0xffff)
  510. continue;
  511. err = sun4v_cpu_state(cpu);
  512. if (err >= 0 &&
  513. err == HV_CPU_STATE_ERROR) {
  514. cpu_list[i] = 0xffff;
  515. cpu_set(cpu, error_mask);
  516. }
  517. }
  518. } else if (unlikely(status != HV_EWOULDBLOCK))
  519. goto fatal_mondo_error;
  520. /* Don't bother rewriting the CPU list, just leave the
  521. * 0xffff and non-0xffff entries in there and the
  522. * hypervisor will do the right thing.
  523. *
  524. * Only advance timeout state if we didn't make any
  525. * forward progress.
  526. */
  527. if (unlikely(!forward_progress)) {
  528. if (unlikely(++retries > 10000))
  529. goto fatal_mondo_timeout;
  530. /* Delay a little bit to let other cpus catch up
  531. * on their cpu mondo queue work.
  532. */
  533. udelay(2 * cnt);
  534. }
  535. } while (1);
  536. local_irq_restore(flags);
  537. if (unlikely(!cpus_empty(error_mask)))
  538. goto fatal_mondo_cpu_error;
  539. return;
  540. fatal_mondo_cpu_error:
  541. printk(KERN_CRIT "CPU[%d]: SUN4V mondo cpu error, some target cpus "
  542. "were in error state\n",
  543. this_cpu);
  544. printk(KERN_CRIT "CPU[%d]: Error mask [ ", this_cpu);
  545. for_each_cpu_mask(i, error_mask)
  546. printk("%d ", i);
  547. printk("]\n");
  548. return;
  549. fatal_mondo_timeout:
  550. local_irq_restore(flags);
  551. printk(KERN_CRIT "CPU[%d]: SUN4V mondo timeout, no forward "
  552. " progress after %d retries.\n",
  553. this_cpu, retries);
  554. goto dump_cpu_list_and_out;
  555. fatal_mondo_error:
  556. local_irq_restore(flags);
  557. printk(KERN_CRIT "CPU[%d]: Unexpected SUN4V mondo error %lu\n",
  558. this_cpu, status);
  559. printk(KERN_CRIT "CPU[%d]: Args were cnt(%d) cpulist_pa(%lx) "
  560. "mondo_block_pa(%lx)\n",
  561. this_cpu, cnt, tb->cpu_list_pa, tb->cpu_mondo_block_pa);
  562. dump_cpu_list_and_out:
  563. printk(KERN_CRIT "CPU[%d]: CPU list [ ", this_cpu);
  564. for (i = 0; i < cnt; i++)
  565. printk("%u ", cpu_list[i]);
  566. printk("]\n");
  567. }
  568. /* Send cross call to all processors mentioned in MASK
  569. * except self.
  570. */
  571. static void smp_cross_call_masked(unsigned long *func, u32 ctx, u64 data1, u64 data2, cpumask_t mask)
  572. {
  573. u64 data0 = (((u64)ctx)<<32 | (((u64)func) & 0xffffffff));
  574. int this_cpu = get_cpu();
  575. cpus_and(mask, mask, cpu_online_map);
  576. cpu_clear(this_cpu, mask);
  577. if (tlb_type == spitfire)
  578. spitfire_xcall_deliver(data0, data1, data2, mask);
  579. else if (tlb_type == cheetah || tlb_type == cheetah_plus)
  580. cheetah_xcall_deliver(data0, data1, data2, mask);
  581. else
  582. hypervisor_xcall_deliver(data0, data1, data2, mask);
  583. /* NOTE: Caller runs local copy on master. */
  584. put_cpu();
  585. }
  586. extern unsigned long xcall_sync_tick;
  587. static void smp_start_sync_tick_client(int cpu)
  588. {
  589. cpumask_t mask = cpumask_of_cpu(cpu);
  590. smp_cross_call_masked(&xcall_sync_tick,
  591. 0, 0, 0, mask);
  592. }
  593. /* Send cross call to all processors except self. */
  594. #define smp_cross_call(func, ctx, data1, data2) \
  595. smp_cross_call_masked(func, ctx, data1, data2, cpu_online_map)
  596. struct call_data_struct {
  597. void (*func) (void *info);
  598. void *info;
  599. atomic_t finished;
  600. int wait;
  601. };
  602. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_lock);
  603. static struct call_data_struct *call_data;
  604. extern unsigned long xcall_call_function;
  605. /**
  606. * smp_call_function(): Run a function on all other CPUs.
  607. * @func: The function to run. This must be fast and non-blocking.
  608. * @info: An arbitrary pointer to pass to the function.
  609. * @nonatomic: currently unused.
  610. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  611. *
  612. * Returns 0 on success, else a negative status code. Does not return until
  613. * remote CPUs are nearly ready to execute <<func>> or are or have executed.
  614. *
  615. * You must not call this function with disabled interrupts or from a
  616. * hardware interrupt handler or from a bottom half handler.
  617. */
  618. static int smp_call_function_mask(void (*func)(void *info), void *info,
  619. int nonatomic, int wait, cpumask_t mask)
  620. {
  621. struct call_data_struct data;
  622. int cpus;
  623. /* Can deadlock when called with interrupts disabled */
  624. WARN_ON(irqs_disabled());
  625. data.func = func;
  626. data.info = info;
  627. atomic_set(&data.finished, 0);
  628. data.wait = wait;
  629. spin_lock(&call_lock);
  630. cpu_clear(smp_processor_id(), mask);
  631. cpus = cpus_weight(mask);
  632. if (!cpus)
  633. goto out_unlock;
  634. call_data = &data;
  635. mb();
  636. smp_cross_call_masked(&xcall_call_function, 0, 0, 0, mask);
  637. /* Wait for response */
  638. while (atomic_read(&data.finished) != cpus)
  639. cpu_relax();
  640. out_unlock:
  641. spin_unlock(&call_lock);
  642. return 0;
  643. }
  644. int smp_call_function(void (*func)(void *info), void *info,
  645. int nonatomic, int wait)
  646. {
  647. return smp_call_function_mask(func, info, nonatomic, wait,
  648. cpu_online_map);
  649. }
  650. void smp_call_function_client(int irq, struct pt_regs *regs)
  651. {
  652. void (*func) (void *info) = call_data->func;
  653. void *info = call_data->info;
  654. clear_softint(1 << irq);
  655. if (call_data->wait) {
  656. /* let initiator proceed only after completion */
  657. func(info);
  658. atomic_inc(&call_data->finished);
  659. } else {
  660. /* let initiator proceed after getting data */
  661. atomic_inc(&call_data->finished);
  662. func(info);
  663. }
  664. }
  665. static void tsb_sync(void *info)
  666. {
  667. struct trap_per_cpu *tp = &trap_block[raw_smp_processor_id()];
  668. struct mm_struct *mm = info;
  669. /* It is not valid to test "currrent->active_mm == mm" here.
  670. *
  671. * The value of "current" is not changed atomically with
  672. * switch_mm(). But that's OK, we just need to check the
  673. * current cpu's trap block PGD physical address.
  674. */
  675. if (tp->pgd_paddr == __pa(mm->pgd))
  676. tsb_context_switch(mm);
  677. }
  678. void smp_tsb_sync(struct mm_struct *mm)
  679. {
  680. smp_call_function_mask(tsb_sync, mm, 0, 1, mm->cpu_vm_mask);
  681. }
  682. extern unsigned long xcall_flush_tlb_mm;
  683. extern unsigned long xcall_flush_tlb_pending;
  684. extern unsigned long xcall_flush_tlb_kernel_range;
  685. extern unsigned long xcall_report_regs;
  686. extern unsigned long xcall_receive_signal;
  687. extern unsigned long xcall_new_mmu_context_version;
  688. #ifdef DCACHE_ALIASING_POSSIBLE
  689. extern unsigned long xcall_flush_dcache_page_cheetah;
  690. #endif
  691. extern unsigned long xcall_flush_dcache_page_spitfire;
  692. #ifdef CONFIG_DEBUG_DCFLUSH
  693. extern atomic_t dcpage_flushes;
  694. extern atomic_t dcpage_flushes_xcall;
  695. #endif
  696. static __inline__ void __local_flush_dcache_page(struct page *page)
  697. {
  698. #ifdef DCACHE_ALIASING_POSSIBLE
  699. __flush_dcache_page(page_address(page),
  700. ((tlb_type == spitfire) &&
  701. page_mapping(page) != NULL));
  702. #else
  703. if (page_mapping(page) != NULL &&
  704. tlb_type == spitfire)
  705. __flush_icache_page(__pa(page_address(page)));
  706. #endif
  707. }
  708. void smp_flush_dcache_page_impl(struct page *page, int cpu)
  709. {
  710. cpumask_t mask = cpumask_of_cpu(cpu);
  711. int this_cpu;
  712. if (tlb_type == hypervisor)
  713. return;
  714. #ifdef CONFIG_DEBUG_DCFLUSH
  715. atomic_inc(&dcpage_flushes);
  716. #endif
  717. this_cpu = get_cpu();
  718. if (cpu == this_cpu) {
  719. __local_flush_dcache_page(page);
  720. } else if (cpu_online(cpu)) {
  721. void *pg_addr = page_address(page);
  722. u64 data0;
  723. if (tlb_type == spitfire) {
  724. data0 =
  725. ((u64)&xcall_flush_dcache_page_spitfire);
  726. if (page_mapping(page) != NULL)
  727. data0 |= ((u64)1 << 32);
  728. spitfire_xcall_deliver(data0,
  729. __pa(pg_addr),
  730. (u64) pg_addr,
  731. mask);
  732. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  733. #ifdef DCACHE_ALIASING_POSSIBLE
  734. data0 =
  735. ((u64)&xcall_flush_dcache_page_cheetah);
  736. cheetah_xcall_deliver(data0,
  737. __pa(pg_addr),
  738. 0, mask);
  739. #endif
  740. }
  741. #ifdef CONFIG_DEBUG_DCFLUSH
  742. atomic_inc(&dcpage_flushes_xcall);
  743. #endif
  744. }
  745. put_cpu();
  746. }
  747. void flush_dcache_page_all(struct mm_struct *mm, struct page *page)
  748. {
  749. void *pg_addr = page_address(page);
  750. cpumask_t mask = cpu_online_map;
  751. u64 data0;
  752. int this_cpu;
  753. if (tlb_type == hypervisor)
  754. return;
  755. this_cpu = get_cpu();
  756. cpu_clear(this_cpu, mask);
  757. #ifdef CONFIG_DEBUG_DCFLUSH
  758. atomic_inc(&dcpage_flushes);
  759. #endif
  760. if (cpus_empty(mask))
  761. goto flush_self;
  762. if (tlb_type == spitfire) {
  763. data0 = ((u64)&xcall_flush_dcache_page_spitfire);
  764. if (page_mapping(page) != NULL)
  765. data0 |= ((u64)1 << 32);
  766. spitfire_xcall_deliver(data0,
  767. __pa(pg_addr),
  768. (u64) pg_addr,
  769. mask);
  770. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  771. #ifdef DCACHE_ALIASING_POSSIBLE
  772. data0 = ((u64)&xcall_flush_dcache_page_cheetah);
  773. cheetah_xcall_deliver(data0,
  774. __pa(pg_addr),
  775. 0, mask);
  776. #endif
  777. }
  778. #ifdef CONFIG_DEBUG_DCFLUSH
  779. atomic_inc(&dcpage_flushes_xcall);
  780. #endif
  781. flush_self:
  782. __local_flush_dcache_page(page);
  783. put_cpu();
  784. }
  785. static void __smp_receive_signal_mask(cpumask_t mask)
  786. {
  787. smp_cross_call_masked(&xcall_receive_signal, 0, 0, 0, mask);
  788. }
  789. void smp_receive_signal(int cpu)
  790. {
  791. cpumask_t mask = cpumask_of_cpu(cpu);
  792. if (cpu_online(cpu))
  793. __smp_receive_signal_mask(mask);
  794. }
  795. void smp_receive_signal_client(int irq, struct pt_regs *regs)
  796. {
  797. clear_softint(1 << irq);
  798. }
  799. void smp_new_mmu_context_version_client(int irq, struct pt_regs *regs)
  800. {
  801. struct mm_struct *mm;
  802. unsigned long flags;
  803. clear_softint(1 << irq);
  804. /* See if we need to allocate a new TLB context because
  805. * the version of the one we are using is now out of date.
  806. */
  807. mm = current->active_mm;
  808. if (unlikely(!mm || (mm == &init_mm)))
  809. return;
  810. spin_lock_irqsave(&mm->context.lock, flags);
  811. if (unlikely(!CTX_VALID(mm->context)))
  812. get_new_mmu_context(mm);
  813. spin_unlock_irqrestore(&mm->context.lock, flags);
  814. load_secondary_context(mm);
  815. __flush_tlb_mm(CTX_HWBITS(mm->context),
  816. SECONDARY_CONTEXT);
  817. }
  818. void smp_new_mmu_context_version(void)
  819. {
  820. smp_cross_call(&xcall_new_mmu_context_version, 0, 0, 0);
  821. }
  822. void smp_report_regs(void)
  823. {
  824. smp_cross_call(&xcall_report_regs, 0, 0, 0);
  825. }
  826. /* We know that the window frames of the user have been flushed
  827. * to the stack before we get here because all callers of us
  828. * are flush_tlb_*() routines, and these run after flush_cache_*()
  829. * which performs the flushw.
  830. *
  831. * The SMP TLB coherency scheme we use works as follows:
  832. *
  833. * 1) mm->cpu_vm_mask is a bit mask of which cpus an address
  834. * space has (potentially) executed on, this is the heuristic
  835. * we use to avoid doing cross calls.
  836. *
  837. * Also, for flushing from kswapd and also for clones, we
  838. * use cpu_vm_mask as the list of cpus to make run the TLB.
  839. *
  840. * 2) TLB context numbers are shared globally across all processors
  841. * in the system, this allows us to play several games to avoid
  842. * cross calls.
  843. *
  844. * One invariant is that when a cpu switches to a process, and
  845. * that processes tsk->active_mm->cpu_vm_mask does not have the
  846. * current cpu's bit set, that tlb context is flushed locally.
  847. *
  848. * If the address space is non-shared (ie. mm->count == 1) we avoid
  849. * cross calls when we want to flush the currently running process's
  850. * tlb state. This is done by clearing all cpu bits except the current
  851. * processor's in current->active_mm->cpu_vm_mask and performing the
  852. * flush locally only. This will force any subsequent cpus which run
  853. * this task to flush the context from the local tlb if the process
  854. * migrates to another cpu (again).
  855. *
  856. * 3) For shared address spaces (threads) and swapping we bite the
  857. * bullet for most cases and perform the cross call (but only to
  858. * the cpus listed in cpu_vm_mask).
  859. *
  860. * The performance gain from "optimizing" away the cross call for threads is
  861. * questionable (in theory the big win for threads is the massive sharing of
  862. * address space state across processors).
  863. */
  864. /* This currently is only used by the hugetlb arch pre-fault
  865. * hook on UltraSPARC-III+ and later when changing the pagesize
  866. * bits of the context register for an address space.
  867. */
  868. void smp_flush_tlb_mm(struct mm_struct *mm)
  869. {
  870. u32 ctx = CTX_HWBITS(mm->context);
  871. int cpu = get_cpu();
  872. if (atomic_read(&mm->mm_users) == 1) {
  873. mm->cpu_vm_mask = cpumask_of_cpu(cpu);
  874. goto local_flush_and_out;
  875. }
  876. smp_cross_call_masked(&xcall_flush_tlb_mm,
  877. ctx, 0, 0,
  878. mm->cpu_vm_mask);
  879. local_flush_and_out:
  880. __flush_tlb_mm(ctx, SECONDARY_CONTEXT);
  881. put_cpu();
  882. }
  883. void smp_flush_tlb_pending(struct mm_struct *mm, unsigned long nr, unsigned long *vaddrs)
  884. {
  885. u32 ctx = CTX_HWBITS(mm->context);
  886. int cpu = get_cpu();
  887. if (mm == current->active_mm && atomic_read(&mm->mm_users) == 1)
  888. mm->cpu_vm_mask = cpumask_of_cpu(cpu);
  889. else
  890. smp_cross_call_masked(&xcall_flush_tlb_pending,
  891. ctx, nr, (unsigned long) vaddrs,
  892. mm->cpu_vm_mask);
  893. __flush_tlb_pending(ctx, nr, vaddrs);
  894. put_cpu();
  895. }
  896. void smp_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  897. {
  898. start &= PAGE_MASK;
  899. end = PAGE_ALIGN(end);
  900. if (start != end) {
  901. smp_cross_call(&xcall_flush_tlb_kernel_range,
  902. 0, start, end);
  903. __flush_tlb_kernel_range(start, end);
  904. }
  905. }
  906. /* CPU capture. */
  907. /* #define CAPTURE_DEBUG */
  908. extern unsigned long xcall_capture;
  909. static atomic_t smp_capture_depth = ATOMIC_INIT(0);
  910. static atomic_t smp_capture_registry = ATOMIC_INIT(0);
  911. static unsigned long penguins_are_doing_time;
  912. void smp_capture(void)
  913. {
  914. int result = atomic_add_ret(1, &smp_capture_depth);
  915. if (result == 1) {
  916. int ncpus = num_online_cpus();
  917. #ifdef CAPTURE_DEBUG
  918. printk("CPU[%d]: Sending penguins to jail...",
  919. smp_processor_id());
  920. #endif
  921. penguins_are_doing_time = 1;
  922. membar_storestore_loadstore();
  923. atomic_inc(&smp_capture_registry);
  924. smp_cross_call(&xcall_capture, 0, 0, 0);
  925. while (atomic_read(&smp_capture_registry) != ncpus)
  926. rmb();
  927. #ifdef CAPTURE_DEBUG
  928. printk("done\n");
  929. #endif
  930. }
  931. }
  932. void smp_release(void)
  933. {
  934. if (atomic_dec_and_test(&smp_capture_depth)) {
  935. #ifdef CAPTURE_DEBUG
  936. printk("CPU[%d]: Giving pardon to "
  937. "imprisoned penguins\n",
  938. smp_processor_id());
  939. #endif
  940. penguins_are_doing_time = 0;
  941. membar_storeload_storestore();
  942. atomic_dec(&smp_capture_registry);
  943. }
  944. }
  945. /* Imprisoned penguins run with %pil == 15, but PSTATE_IE set, so they
  946. * can service tlb flush xcalls...
  947. */
  948. extern void prom_world(int);
  949. void smp_penguin_jailcell(int irq, struct pt_regs *regs)
  950. {
  951. clear_softint(1 << irq);
  952. preempt_disable();
  953. __asm__ __volatile__("flushw");
  954. prom_world(1);
  955. atomic_inc(&smp_capture_registry);
  956. membar_storeload_storestore();
  957. while (penguins_are_doing_time)
  958. rmb();
  959. atomic_dec(&smp_capture_registry);
  960. prom_world(0);
  961. preempt_enable();
  962. }
  963. void __init smp_tick_init(void)
  964. {
  965. boot_cpu_id = hard_smp_processor_id();
  966. }
  967. /* /proc/profile writes can call this, don't __init it please. */
  968. int setup_profiling_timer(unsigned int multiplier)
  969. {
  970. return -EINVAL;
  971. }
  972. static void __init smp_tune_scheduling(void)
  973. {
  974. unsigned int smallest = ~0U;
  975. int i;
  976. for (i = 0; i < NR_CPUS; i++) {
  977. unsigned int val = cpu_data(i).ecache_size;
  978. if (val && val < smallest)
  979. smallest = val;
  980. }
  981. /* Any value less than 256K is nonsense. */
  982. if (smallest < (256U * 1024U))
  983. smallest = 256 * 1024;
  984. max_cache_size = smallest;
  985. if (smallest < 1U * 1024U * 1024U)
  986. printk(KERN_INFO "Using max_cache_size of %uKB\n",
  987. smallest / 1024U);
  988. else
  989. printk(KERN_INFO "Using max_cache_size of %uMB\n",
  990. smallest / 1024U / 1024U);
  991. }
  992. /* Constrain the number of cpus to max_cpus. */
  993. void __init smp_prepare_cpus(unsigned int max_cpus)
  994. {
  995. int i;
  996. if (num_possible_cpus() > max_cpus) {
  997. for_each_possible_cpu(i) {
  998. if (i != boot_cpu_id) {
  999. cpu_clear(i, phys_cpu_present_map);
  1000. cpu_clear(i, cpu_present_map);
  1001. if (num_possible_cpus() <= max_cpus)
  1002. break;
  1003. }
  1004. }
  1005. }
  1006. cpu_data(boot_cpu_id).udelay_val = loops_per_jiffy;
  1007. smp_tune_scheduling();
  1008. }
  1009. void __devinit smp_prepare_boot_cpu(void)
  1010. {
  1011. }
  1012. void __devinit smp_fill_in_sib_core_maps(void)
  1013. {
  1014. unsigned int i;
  1015. for_each_possible_cpu(i) {
  1016. unsigned int j;
  1017. if (cpu_data(i).core_id == 0) {
  1018. cpu_set(i, cpu_core_map[i]);
  1019. continue;
  1020. }
  1021. for_each_possible_cpu(j) {
  1022. if (cpu_data(i).core_id ==
  1023. cpu_data(j).core_id)
  1024. cpu_set(j, cpu_core_map[i]);
  1025. }
  1026. }
  1027. for_each_possible_cpu(i) {
  1028. unsigned int j;
  1029. if (cpu_data(i).proc_id == -1) {
  1030. cpu_set(i, cpu_sibling_map[i]);
  1031. continue;
  1032. }
  1033. for_each_possible_cpu(j) {
  1034. if (cpu_data(i).proc_id ==
  1035. cpu_data(j).proc_id)
  1036. cpu_set(j, cpu_sibling_map[i]);
  1037. }
  1038. }
  1039. }
  1040. int __cpuinit __cpu_up(unsigned int cpu)
  1041. {
  1042. int ret = smp_boot_one_cpu(cpu);
  1043. if (!ret) {
  1044. cpu_set(cpu, smp_commenced_mask);
  1045. while (!cpu_isset(cpu, cpu_online_map))
  1046. mb();
  1047. if (!cpu_isset(cpu, cpu_online_map)) {
  1048. ret = -ENODEV;
  1049. } else {
  1050. /* On SUN4V, writes to %tick and %stick are
  1051. * not allowed.
  1052. */
  1053. if (tlb_type != hypervisor)
  1054. smp_synchronize_one_tick(cpu);
  1055. }
  1056. }
  1057. return ret;
  1058. }
  1059. void __init smp_cpus_done(unsigned int max_cpus)
  1060. {
  1061. unsigned long bogosum = 0;
  1062. int i;
  1063. for_each_online_cpu(i)
  1064. bogosum += cpu_data(i).udelay_val;
  1065. printk("Total of %ld processors activated "
  1066. "(%lu.%02lu BogoMIPS).\n",
  1067. (long) num_online_cpus(),
  1068. bogosum/(500000/HZ),
  1069. (bogosum/(5000/HZ))%100);
  1070. }
  1071. void smp_send_reschedule(int cpu)
  1072. {
  1073. smp_receive_signal(cpu);
  1074. }
  1075. /* This is a nop because we capture all other cpus
  1076. * anyways when making the PROM active.
  1077. */
  1078. void smp_send_stop(void)
  1079. {
  1080. }
  1081. unsigned long __per_cpu_base __read_mostly;
  1082. unsigned long __per_cpu_shift __read_mostly;
  1083. EXPORT_SYMBOL(__per_cpu_base);
  1084. EXPORT_SYMBOL(__per_cpu_shift);
  1085. void __init real_setup_per_cpu_areas(void)
  1086. {
  1087. unsigned long goal, size, i;
  1088. char *ptr;
  1089. /* Copy section for each CPU (we discard the original) */
  1090. goal = PERCPU_ENOUGH_ROOM;
  1091. __per_cpu_shift = PAGE_SHIFT;
  1092. for (size = PAGE_SIZE; size < goal; size <<= 1UL)
  1093. __per_cpu_shift++;
  1094. ptr = alloc_bootmem_pages(size * NR_CPUS);
  1095. __per_cpu_base = ptr - __per_cpu_start;
  1096. for (i = 0; i < NR_CPUS; i++, ptr += size)
  1097. memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
  1098. /* Setup %g5 for the boot cpu. */
  1099. __local_per_cpu_offset = __per_cpu_offset(smp_processor_id());
  1100. }