smp.c 34 KB

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