smp.c 31 KB

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