smp.c 31 KB

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