smp.c 28 KB

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